Objective c 无法实现KVO以跟踪布尔属性
我已经成功地为我的keyPath@isFinished实现了KVO,但是我不能对另一个属性执行同样的操作:isoflinecontentnil; 它的更改未注册 对象:Objective c 无法实现KVO以跟踪布尔属性,objective-c,key-value-observing,Objective C,Key Value Observing,我已经成功地为我的keyPath@isFinished实现了KVO,但是我不能对另一个属性执行同样的操作:isoflinecontentnil; 它的更改未注册 对象: @property (nonatomic) BOOL isFinished; @property (nonatomic) BOOL isOfflineContentNil; @end @implementation DataManager -(instancetype)init { self = [super ini
@property (nonatomic) BOOL isFinished;
@property (nonatomic) BOOL isOfflineContentNil;
@end
@implementation DataManager
-(instancetype)init {
self = [super init];
if (self) {
[[NSUserDefaults standardUserDefaults] setObject:@[@{@"website" : @"TechCrunch", @"title" : @"New iPhone comming", @"authors" : @"some", @"date" : @"06/08/2014"},@{@"website" : @"TheVerge", @"title" : @"Macbook line refreshed", @"authors" : @"john", @"date" : @"16/09/2014"}] forKey:@"savedNews"];
self.isOfflineContentNil = YES;
NSMutableArray *userDefault = [[NSUserDefaults standardUserDefaults] objectForKey:@"savedNews"];
if (userDefault) {
self.savedForLaterNews = userDefault;
self.isOfflineContentNil = NO;
}
dispatch_async(kBgQueue, ^{
NSData *data = [NSData dataWithContentsOfURL:kLatestNewsURL];
[self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});
self.isFinished = NO;
}
return self;
}
-(void)fetchedData:(NSData *)responseData {
NSError *error;
self.json = [NSJSONSerialization JSONObjectWithData:responseData
options:kNilOptions
error:&error];
self.currentNews = [self.json mutableCopy];
self.isFinished = YES;
[self sortJSONInformation];
}
听众:
self.dataManager = [DataManager new];
[self.dataManager addObserver:self forKeyPath:@"isFinished" options:NSKeyValueObservingOptionOld context:nil];
[self.dataManager addObserver:self forKeyPath:@"isOfflineContentNil" options:NSKeyValueObservingOptionOld context:nil];
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"isFinished"]) {
[self animateViewDismiss:self.loadingView];
} else if ([keyPath isEqualToString:@"isOfflineContentNil"]) {
self.isCurrentNews = NO;
[self animateViewDismiss:self.loadingView];
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
这是一位杰出的领袖,他是一位伟大的领袖。但是,在最低限度的利益下,我们必须在公共消费品的基础上,继续履行我们的义务。Duis autem vel eum iriure发现我需要将观察选项修改为
options:NSKeyValueObservingOptionInitial
非常罕见,但适用于这种情况。发现我需要将观察选项修改为
options:NSKeyValueObservingOptionInitial
非常罕见,但适用于这种情况。您没有在if-else中添加大括号以粘贴到此处,是吗?我担心if self.isCurrentNews上的else-if for IsoflineContentNil会附加到if self.isCurrentNews上,在这种情况下,if-else不会做你希望的事情。@stevesliva我编辑了它,但仍然不起作用。顺便说一句,没有调用observeValue。您没有在if ELSE中添加大括号以粘贴到此处,是吗?我担心if self.isCurrentNews上的else-if for IsoflineContentNil会附加到if self.isCurrentNews上,在这种情况下,if-else不会做你希望的事情。@stevesliva我编辑了它,但仍然不起作用。顺便说一句,没有调用observeValue.Nice。谢谢你发布答案。我认为这个答案是错误的。NSKeyValueObservingOptionNew如何?一般来说,它应该是两个选项:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew。这样,您的observer代码不仅能够处理observable属性发生更改的情况,而且还能够处理您需要进行初始设置的情况,因为在添加observable.Nice时,observable属性(最重要的是observable属性)已经有了一些值。谢谢你发布答案。我认为这个答案是错误的。NSKeyValueObservingOptionNew如何?一般来说,它应该是两个选项:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew。这样,您的observer代码不仅能够处理observable属性发生更改的情况,而且还能够处理您需要进行初始设置的情况,因为在添加observable属性时,observable属性大多数都已经有了一些值。