KVO 自定义对象属性的可变数组

KVO on mutable array of custom objects properties

我知道这可能是一个重复的问题,但是当我尝试时我没有得到更改的属性。我遵循了几个 KVO + Array SO 答案。但是我仍然缺少一些位,因为它在我的情况下不起作用。

User.h:

@interface User : NSObject
...
@property(nonatomic, assign) double latitude;
@property(nonatomic, assign) double longitude;
...
@end

SingletonClass.h:

@interface SingletonClass : NSObject
....
@property(nonatomic, readonly) NSMutableArray *remoteUsers;//Stores user objects
...
@end

SingletonClass.m

@implementation SingletonClass

for (int i = 0; i < _remoteUsers.count; i++) {
            index = [NSIndexSet indexSetWithIndex:i];
            User *rmtUser = _remoteUsers[i];
            if ([rmtUser.roomJid isEqualToString:occupantJID.description]) {
                [self willChange:NSKeyValueChangeReplacement valuesAtIndexes:index forKey:@"remoteUsers"];
                rmtUser.latitude = user.latitude;
                rmtUser.longitude = user.longitude;
                [self didChange:NSKeyValueChangeReplacement valuesAtIndexes:index forKey:@"remoteUsers"];
                isUserInList = YES;
                break;
            }
        }

MainViewClass.h:这里我想更新用户在地图上的位置。

MainViewClass.m:

@implementation MainViewClass
....
[SingletonClass addObserver:self forKeyPath:@"remoteUsers" options:NSKeyValueObservingOptionNew context:nil];
....

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if([keyPath isEqualToString:@"remoteUsers"]) {
        User *remoteUser = ((SingletonClass *)object).remoteUsers[0];
        NSLog(@"THE LATITUDE OBSERVED IS %f", remoteUser.latitude);
        NSLog(@"THE LONGITUDE OBSERVED IS %f", remoteUser.longitude);
    }
}

这里我得到了完整的用户对象而不是 changed/updated 属性。我哪里做错了吗?

如果您将代码更改为:

怎么样?
[self willChangeValueForKey:@"remoteUsers"];
rmtUser.latitude = user.latitude;
rmtUser.longitude = user.longitude;
[self didChangeValueForKey:@"remoteUsers"];

正如@A-Live 提到的那样,它按预期工作。 post.

中没有实际代码中的类型