开发者

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".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜