objective-c 如何将 nil 添加到 nsmutablearray?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2057910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to add nil to nsmutablearray?
提问by stefanosn
NSArray *array = [[NSArray alloc] initWithObjects:@"ΕΛΤΑ",
@"ΕΛΤΑ COURIER", @"ACS", @"ACS ΕΞΩΤΕΡΙΚΟ",
@"DHL", @"INTERATTICA", @"SPEEDEX",
@"UPS", @"ΓΕΝΙΚΗ ΤΑΧΥΔΡΟΜΙΚΗ", @"ΜΕΤΑΦΟΡΙΚΕΣ ΕΞΩΤΕΡΙΚΟΥ", nil];
This is working because it has nil at the end.
这是有效的,因为它最后为零。
But I add objects like this: addObject:nameetc...
So at the end I have to add nil I do this addObhect:nil but when I run the app it still crashes at cellForRowAtIndexPath:
但是我添加了这样的对象:addObject:name等等......所以最后我必须添加 nil 我这样做 addObhect:nil 但是当我运行应用程序时它仍然崩溃cellForRowAtIndexPath:
how can I do this work?
我该如何做这项工作?
Ok, I dont have to add nil
好的,我不必添加 nil
What is the reason that my app crashes then?
我的应用程序崩溃的原因是什么?
采纳答案by mr-sk
You can't add nil when you're calling addObject.
调用时不能添加 nil addObject。
回答by Adrian Kosmaczewski
If you must add a nilobject to a collection, use the NSNullclass:
如果必须将nil对象添加到集合中,请使用NSNull该类:
The NSNull class defines a singleton object used to represent null values in collection objects (which don't allow nil values).
NSNull 类定义了一个单例对象,用于表示集合对象中的空值(不允许空值)。
Assuming "array" is of type NSMutableArray:
假设“数组”是 NSMutableArray 类型:
....
[array addObject:[NSNumber numberWithInt:2];
[array addObject:@"string"];
[array addObject:[NSNull null]];
回答by Mike Weller
You don't need to call [addObject:nil]
你不需要打电话 [addObject:nil]
The nilin initWithObjects:is only there to tell the method where the list ends, because of how C varargswork. When you add objects one-by-one with addObject:you don't need to add a nil.
在nil中initWithObjects:是唯一没有告诉方法,其中,列表结束,因为C如何,varargs工作。当您一一添加对象时,addObject:您不需要添加 nil。
回答by David Grant
回答by mert
You need to add NSNullclass and the best way to do it is like this:
您需要添加NSNull类,最好的方法是这样的:
NSArray *array = @[ @"string", @42, [NSNull null] ];
I personally recommend to use a specific value like 0instead of null or nil in your design of your code, but sometimes you need to add null.
我个人建议0在您的代码设计中使用特定值,而不是 null 或 nil,但有时您需要添加 null。
There is a good explanation from this Apple reference.
这个苹果参考有一个很好的解释。
回答by Tomen
nil is used to terminate the array
nil 用于终止数组
回答by mouviciel
nilis not an object that you can add to an array: An array cannot contain nil. This is why addObject:nilcrashes.
nil不是可以添加到数组的对象:数组不能包含nil. 这就是addObject:nil崩溃的原因。
回答by Manjuhere
pass your object through this method when adding to array to avoid attempt to insert nil object from objectscrashes.
添加到数组时通过此方法传递您的对象,以避免尝试从对象崩溃中插入 nil 对象。
-(id) returnNullIfNil:(id) obj {
return (obj == nil) ? ([NSNull null]) : (obj);
}
[NSNull null] returns an null object which represents nil.
[NSNull null] 返回一个表示 nil 的 null 对象。
回答by TechZen
You can't add an object to an NSArraybecause that class is immutable. You have to use NSMutableArrayif you want to change the array after it is created.
您不能向 an 添加对象,NSArray因为该类是不可变的。NSMutableArray如果要在创建数组后更改数组,则必须使用。

