【发布时间】:2015-01-10 15:20:51
【问题描述】:
我有一个ServiceFacade 类,其中包含用于与后端服务通信的类方法。
在那个ServiceFacade 类上,我有一个返回NSMutableDictionary 的静态方法,我在其中保持当前ServiceFacade 的下载操作。
我想在AppDelegate 或任何其他地方观察NSMutableDictionary 的变化。应用委托似乎没有响应
- (void)addObserver:(NSObject *)anObserver
forKeyPath:(NSString *)keyPath
options:(NSKeyValueObservingOptions)options
context:(void *)context{
}
返回NSMutableDictionary的方法:
+(NSMutableDictionary *)downloadOperations
{
if (_downloadOperations)
{
return _downloadOperations;
}
[_downloadOperations addObserver:[AppDelegate sharedAppDelegate] forKeyPath:@"downloadOperationsDict" options:0 context:NULL];
_downloadOperations = [NSMutableDictionary dictionary];
return _downloadOperations;
}
有什么想法吗?
【问题讨论】:
-
KVO 只有在
_downloadOperations指针改变时才会被触发。改变_downloadOperations指向的字典内容将不会触发 KVO 通知。 -
不,只有在
-setDownloadOperation被执行时才会触发。这就是问题所在。 -
是的。抱歉,这就是我的暗示,但解释得很糟糕。
标签: ios objective-c observer-pattern nsmutabledictionary key-value-observing