I have a situation where I'd like to be able to maintain an array of pointers that might all possibly point to nil.
Equipment *equipment[19];
However, I've found that I cannot set an array of pointers, or a double-pointer, as a property of an object.
My workaround when I can't use C-style arrays is to use the NSArray objects. So I attempted to do something like the following:
NSMutableArray *equipment = [NSMutableArray arrayWithCapacity: NUM_EQUIPSLOTS];
for (int i=0; i<NUM_EQUIPSLOTS; i++) {
[equipment setObject: nil atIndexedSubscript: i];
}
The idea here being I have an array of empty pointers that will later point to stuff.
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(0x246e012 0x1e4be7e 0x2421b6a 0x2421a20 0xebd02 0xde82b 0xc9d5a 0xcb6bd 0x4d525 0xc94fa 0xc8b6a 0xa18157 0xa18747 0xa1994b 0xa2acb5 0xa2bbeb 0xa1d698 0x3176df9 0x3176ad0 0x23e3bf5 0x23e3962 0x2414bb6 0x2413f44 0x2413e1b 0xa1917a 0xa1affc 0xc8526 0x1fa5)
libc++abi.dylib: terminate called throwing an exception
I know I can do this very easily using C-style arrays, and with individual objects. I'd rather do it this way than something dumb like:
Equipment *equipment0 = nil;
Equipment *equipment1 = nil;
Equipment *equipment2 = nil;
// ...
Equipment *equipment18 = nil;
This probably has to do with the structure of the NSArray model itself. Would someone explain to me why this is, and why I can't simply add or set nil objects in NSArray? Thank you in advance.