Can a NSMutableArray contain nil?
I'm learning Objective-C and I have a question about NSArray. Can it contain nil? I read somewhere an NSArray cannot contain 开发者_StackOverflownil. Is this the same for NSMutableArray?
Neither can contain nil. They'll raise an exception if you try to put it in. If you want to represent the idea of "nothing" in an array, set or dictionary, you can use NSNull.
Since everybody seems so convinced that an NSArray
cannot contain nil I want to add my two cents:
CFMutableArrayRef array = CFArrayCreateMutable(NULL, 1, NULL);
CFArrayAppendValue(array, NULL);
NSLog(@"array: %@", array);
Remember that CFMutableArrayRef
is toll-free bridged to its Cocoa counterpart NSMutableArray
.
I admit that this is a constructed case and probably seldom in Cocoa.
You cannot put a nil
inside an NSMutableArray
. See "NSMutableArray Class Doc".
精彩评论