我在代码中使用kvc
。
[self addObserver:self forKeyPath:@"type" options:NSKeyValueObservingOptionNew |NSKeyValueObservingOptionOld context:nil];
[self addObserver:self forKeyPath:@"location" options:NSKeyValueObservingOptionNew |NSKeyValueObservingOptionOld context:nil];
我也在这里采取这个功能:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
if ([keyPath isEqualToString:@"location"] || [keyPath isEqualToString:@"type"]) {
// refresh data
if (self.currentTableView == _sellTableView) {
[self addSellDataSourceWithFlag:1];
}else {
[self addBuyDataSourceWithFlag:1];
}
}
}
在dealloc中,我删除了它们。
- (void)dealloc {
[self removeObserver:self forKeyPath:@"location"];
[self removeObserver:self forKeyPath:@"type"];
}
但是,当我退出我的应用时:
// exit
- (IBAction)exitButtonPress:(UIButton *)sender {
self.navigationController.navigationBar.hidden = YES;
self.tabBarController.tabBar.hidden = YES;
[[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"user_isLogin"];
[[NSUserDefaults standardUserDefaults] synchronize];
[self.tabBarController dismissViewControllerAnimated:YES completion:^{
[self.tabBarController setSelectedIndex:0];
}];
}
出现问题,All Exceptions
出现了一行:
[self removeObserver:self forKeyPath:@"location"];
问题信息如下:
libc++abi.dylib: terminate_handler unexpectedly threw an exception
当我bt
时:
(lldb) bt
* thread #1: tid = 0x387c7, 0x000000010a6dbf06 libsystem_kernel.dylib`__pthread_kill + 10, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
frame #0: 0x000000010a6dbf06 libsystem_kernel.dylib`__pthread_kill + 10
frame #1: 0x000000010a6a34ec libsystem_pthread.dylib`pthread_kill + 90
frame #2: 0x000000010a43dcec libsystem_c.dylib`abort + 129
frame #3: 0x000000010a23c051 libc++abi.dylib`abort_message + 257
frame #4: 0x000000010a25f292 libc++abi.dylib`std::__terminate(void (*)()) + 44
frame #5: 0x000000010a25eef9 libc++abi.dylib`__cxa_rethrow + 99
frame #6: 0x0000000107b3cf5e libobjc.A.dylib`objc_exception_rethrow + 40
frame #7: 0x000000010831e1b4 CoreFoundation`CFRunLoopRunSpecific + 676
frame #8: 0x000000010a83ead2 GraphicsServices`GSEventRunModal + 161
frame #9: 0x0000000108cc3f09 UIKit`UIApplicationMain + 171
* frame #10: 0x00000001047d5b8f E农通`main(argc=1, argv=0x00007fff5b562638) + 111 at main.m:14
frame #11: 0x000000010a39792d libdyld.dylib`start + 1
frame #12: 0x000000010a39792d libdyld.dylib`start + 1
如何使用kvc
解决此问题?
答案 0 :(得分:1)
LeoMaer,
您可能尝试在添加观察者之前删除观察者。所以解决方案是确保在删除观察者之前添加了它。
尝试在你的类的init或viewDidLoad中添加你的观察者,然后在dealloc中删除它。
另一方面,如果你有一些特定的要求,你必须根据条件或类似的东西添加它,以避免崩溃。
这将删除观察者,如果添加了其他异常将被捕获并且不会崩溃
try:
#some code which passes parameters to constructor of A if this fails.
except A as e:
#here I want e as a list instead of string.
答案 1 :(得分:1)
要解决这个问题,我有一个解决方案。
获取@property NSMutableArray *observe_arr;
,如果是[self addObserver:self forKeyPath:@"observeName" options:NSKeyValueObservingOptionNew |NSKeyValueObservingOptionOld context:nil];
将observeName
添加到observe_arr
。
在dealloc中:
- (void)dealloc {
while (self.observe_arr.firstObject) {
[self removeObserver:self forKeyPath:self.observe_arr.firstObject];
}
}
顺便说一下,非常感谢Sandeep Bhandari的帮助。