无法将对象添加到NSMutableArray

时间:2023-01-08 22:24:21

Im having truble adding objects to my 2 NSMutableArrays. The data comes from a database and I know that my parsing is correct because I get valid outputs when I use the NSLog. However I can't figur out how to add the 2 different objects to my 2 different NSMutableArrays. Here is my code

我正在向我的2个NSMutableArrays添加对象。数据来自数据库,我知道我的解析是正确的,因为当我使用NSLog时,我得到有效的输出。但是我无法想出如何将2个不同的对象添加到我的2个不同的NSMutableArrays中。这是我的代码

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {

     allDataDictionary = [NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
     feed = [allDataDictionary objectForKey:@"feed"];
     arrayOfEntry = [feed objectForKey:@"entry"];

     for (NSDictionary *dictionary in arrayOfEntry) {

         NSDictionary *title = [dictionary objectForKey:@"title"];
         NSString     *labelTitle = [title objectForKey:@"label"];

         [arrayLabel addObject:labelTitle];

         NSDictionary *summary = [dictionary objectForKey:@"summary"];
         NSString     *labelSummary = [summary objectForKey:@"label"]; 

         [arraySummary addObject:labelSummary]; //This line makes the application crash

     }

}

for some reason when I want to add labelSummary to arraySummary I get this error:

出于某种原因,当我想将labelSummary添加到arraySummary时,我收到此错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

Any help is appreciated.

任何帮助表示赞赏。

2 个解决方案

#1


2  

Your parsing is indeed correct. However, when the parser comes across an empty field it returns nil. The problem is the that NSArrays cannot accept nils, because nil is not an object, it's equivalent to 0. Therefore, you most add an object. This the role of NSNull.

你的解析确实是正确的。但是,当解析器遇到空字段时,它返回nil。问题是NSArrays不能接受nils,因为nil不是一个对象,它相当于0.因此,你最常添加一个对象。这就是NSNull的作用。

Must test to see if the parser returns nil, and if so add [NSNull null].

必须测试以查看解析器是否返回nil,如果是,则添加[NSNull null]。

NSString* labelSummary = [summary objectForKey:@"label"]; 

[arraySummary addObject:(labelSummary!=nil)?labelSummary:[NSNull null];

#2


1  

The error message tells you that one of the objects you are trying to add to the array is nil.

错误消息告诉您,您尝试添加到阵列的其中一个对象是nil。

You have to replace

你必须更换

[arrayLabel addObject:labelTitle];

with

if (labelTitle != nil) {
   [arrayLabel addObject:labelTitle];
}

and

[arraySummary addObject:labelSummary];

with

if (labelSummary != nil) {
   [arraySummary addObject:labelSummary];
}

If you really need to include a nil object, then use NSNull.

如果你真的需要包含一个nil对象,那么使用NSNull。

#1


2  

Your parsing is indeed correct. However, when the parser comes across an empty field it returns nil. The problem is the that NSArrays cannot accept nils, because nil is not an object, it's equivalent to 0. Therefore, you most add an object. This the role of NSNull.

你的解析确实是正确的。但是,当解析器遇到空字段时,它返回nil。问题是NSArrays不能接受nils,因为nil不是一个对象,它相当于0.因此,你最常添加一个对象。这就是NSNull的作用。

Must test to see if the parser returns nil, and if so add [NSNull null].

必须测试以查看解析器是否返回nil,如果是,则添加[NSNull null]。

NSString* labelSummary = [summary objectForKey:@"label"]; 

[arraySummary addObject:(labelSummary!=nil)?labelSummary:[NSNull null];

#2


1  

The error message tells you that one of the objects you are trying to add to the array is nil.

错误消息告诉您,您尝试添加到阵列的其中一个对象是nil。

You have to replace

你必须更换

[arrayLabel addObject:labelTitle];

with

if (labelTitle != nil) {
   [arrayLabel addObject:labelTitle];
}

and

[arraySummary addObject:labelSummary];

with

if (labelSummary != nil) {
   [arraySummary addObject:labelSummary];
}

If you really need to include a nil object, then use NSNull.

如果你真的需要包含一个nil对象,那么使用NSNull。