Iphone - mutableArray cannot store nil objects

I have a mutable array that is saved and stores multiple objects. At some point, one object may become null. When this happens, the application crashes because arrays cannot have null objects. Imagine something like

[object1, object2, object3, nil];

then object2 = nil

[object1, nil, object3, nil];

which is impossible because nil is the end of the array token. So how can I solve this? Thanks for any help.

+3
source share
1 answer

Use [NSNull null]if you need to save an empty placeholder.

For instance:

NSArray * myArray = [NSArray arrayWithObjects:obj1, [NSNull null], obj3, nil];

myArray 3 . , , , Null singleton:

id object = [myArray objectAtIndex:anIndex];
if (object == [NSNull null]) {
  //it the null object
} else {
  //it a normal object
}

EDIT ( )

@Mike , , .

:

id obj = ...;

obj . . , NSLog(@"%p", obj), - 0x1234567890. obj , , . , 0x1234567890. , : obj = nil;, . - 0x1234567890.

+9

Source: https://habr.com/ru/post/1740190/


All Articles