Search code examples
iosnsmutablearraykey-value-codingkvcnsnull

Remove NSNull values in a KVC generated NSMutableArray


Here's a little test:

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);

The log gives:

(
    12345,
    2345,
    345,
    "<null>"
)

That's great, now, I wish to get rid of the NSNull values... According to How will I be able to remove [NSNull Null] objects from NSMutableArray? this is the way:

[test2 removeObjectIdenticalTo:[NSNull null]];
NSLog(@"%@",test2);

Unfortunately, it crashes:

*** 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

So, I know many ways of removing NSNull instances from the array (predicates, indexesPassingTests, etc.) but I really want to understand why this fails with so little explanations.

Thanks in advance.

Edit: To prevent the proxy problem given in Ian's answer the simplest solution is:

NSMutableArray * test2 = [[test valueForKeyPath:@"centers.id"] mutableCopy];

Solution

  • The NSMutableArray returned from mutableArrayValueForKeyPath is not a copy of the values, but instead a proxy mutable array that does a lookup to the original object on each access.

    Telling it to remove objects will act upon your original object. Telling it to remove objects whose centers.id is [NSNull null] causes a failure because those objects do not respond to the key-value mapping for id. It's strange to think that it would have given you any result in the first place, but you cannot use this [NSNull null] result in the way that you want.

    If you just want the values and you don't want any operations available to allow modification of the original object, I suggest that you construct your array in a different way to begin with; perhaps with a predicate.