【发布时间】:2015-02-16 17:40:12
【问题描述】:
这是一个小测试:
NSDictionary * test = @ { @"centers" : @[
@{ @"id":@"12345", @"favorite":@NO },
@{ @"id":@"2345", @"favorite":@NO },
@{ @"id":@"345", @"favorite":@YES },
@{ @"favorite":@NO }
]};
NSMutableArray * test2 = [test mutableArrayValueForKeyPath:@"centers.id"];
NSLog(@"%@",test2);
日志给出:
(
12345,
2345,
345,
"<null>"
)
太好了,现在,我希望摆脱 NSNull 值...根据How will I be able to remove [NSNull Null] objects from NSMutableArray?,这就是方式:
[test2 removeObjectIdenticalTo:[NSNull null]];
NSLog(@"%@",test2);
不幸的是,它崩溃了:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSDictionaryI 0x7fe2ee01e970> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key id.'
*** First throw call stack:
(
0 CoreFoundation 0x000000010e47df35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010e116bb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010e47db79 -[NSException raise] + 9
3 Foundation 0x000000010dcb37b3 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 259
4 Foundation 0x000000010dd44be0 -[NSArray(NSKeyValueCoding) setValue:forKey:] + 186
5 Foundation 0x000000010dd471cc -[NSKeyValueSlowMutableArray removeObjectAtIndex:] + 89
6 CoreFoundation 0x000000010e3d75a3 -[NSMutableArray removeObjectIdenticalTo:] + 131
7 TestImageSizeReduction 0x000000010dbe6d35 -[ViewController viewDidLoad] + 1397
8 UIKit 0x000000010ebd2a90 -[UIViewController loadViewIfRequired] + 738
9 UIKit 0x000000010ebd2c8e -[UIViewController view] + 27
10 UIKit 0x000000010eaf1ca9 -[UIWindow addRootViewControllerViewIfPossible] + 58
11 UIKit 0x000000010eaf2041 -[UIWindow _setHidden:forced:] + 247
12 UIKit 0x000000010eafe72c -[UIWindow makeKeyAndVisible] + 42
13 UIKit 0x000000010eaa9061 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2628
14 UIKit 0x000000010eaabd2c -[UIApplication _runWithMainScene:transitionContext:completion:] + 1350
15 UIKit 0x000000010eaaabf2 -[UIApplication workspaceDidEndTransaction:] + 179
16 FrontBoardServices 0x0000000111f582a3 __31-[FBSSerialQueue performAsync:]_block_invoke + 16
17 CoreFoundation 0x000000010e3b353c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
18 CoreFoundation 0x000000010e3a9285 __CFRunLoopDoBlocks + 341
19 CoreFoundation 0x000000010e3a9045 __CFRunLoopRun + 2389
20 CoreFoundation 0x000000010e3a8486 CFRunLoopRunSpecific + 470
21 UIKit 0x000000010eaaa669 -[UIApplication _run] + 413
22 UIKit 0x000000010eaad420 UIApplicationMain + 1282
23 TestImageSizeReduction 0x000000010dbe81f3 main + 115
24 libdyld.dylib 0x0000000110c50145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
所以,我知道从数组中删除 NSNull 实例的许多方法(谓词、indexsPassingTests 等),但我真的很想了解为什么这会失败,但解释很少。
提前致谢。
编辑: 为了防止伊恩回答中给出的代理问题,最简单的解决方案是:
NSMutableArray * test2 = [[test valueForKeyPath:@"centers.id"] mutableCopy];
【问题讨论】:
-
这是一个不可变的字典,因此您无法更改其中存储的内容。尝试将其更改为 NSMutableDictionary 并遍历所有键并删除等于
[NSNull null]的键。 -
感谢您的评论,但调用是在数组上,而不是在字典上,不是吗?
-
试试我的答案应该可以。
标签: ios nsmutablearray key-value-coding kvc nsnull