这是一个小测试:

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实例的方法(谓词,indexesPassingTests等),但是我真的很想了解为什么这样做失败,但解释却很少。

提前致谢。

编辑:
为了防止Ian回答中给出的代理问题,最简单的解决方案是:
NSMutableArray * test2 = [[test valueForKeyPath:@"centers.id"] mutableCopy];

最佳答案

NSMutableArray返回的mutableArrayValueForKeyPath不是值的副本,而是一个代理可变数组,它在每次访问时都对原始对象进行查找。

告诉它删除对象将对您的原始对象起作用。告诉它删除centers.id[NSNull null]的对象会导致失败,因为这些对象不响应id的键-值映射。奇怪的是,它首先会给您带来任何结果,但是您无法以所需的方式使用此[NSNull null]结果。

如果您只需要这些值,并且不希望进行任何允许修改原始对象的操作,则建议您以不同的方式构造数组。也许带有谓词。

关于ios - 删除KVC生成的NSMutableArray中的NSNull值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28547215/

10-13 05:42