I have a mutable array that is retained and storing several objects. At some point, one object may become nil. When this happens the app will crash, because arrays cannot have nil objects. Imagine something like
[object1, object2, object3, nil];
then, object2 = nil
[object1, nil, object3, nil];
that is not possible because nil is the end of array marker. So, how can I solve that? thanks for any help.
Use
[NSNull null]if you have to store an empty placeholder object.For example:
myArraywill contain 3 objects. When you retrieve the object, you can do a simple pointer equality test to see if it’s the Null singleton:EDIT (responding to a comment)
@Mike I think you’re getting confused with what’s actually going on.
If you have:
Then
objcontains an address. It does not contain an object. As such, if you doNSLog(@"%p", obj), it’ll print something like0x1234567890. When you putobjinto the array, it’s not copying the object, it’s copying the address of the object. So the array actually contains0x1234567890. Therefore, when you later do:obj = nil;, you’re only affecting the pointer outside of the array. The array will still contain0x1234567890.