我将数组添加到nsmutabledictionary但它给出了0计数 我在viewDidLoad方法中分配了字典。
任何人都可以帮帮我..
感谢。
- (void)creatingDictionary{
songsDict = nil;
NSMutableArray *hrSongs = [[NSMutableArray alloc]init];
for(int i=0;i<[onAirSongs count];i++){
for(int j=i;j<[onAirSongs count];j++){
Song *a1 =(Song *)[onAirSongs objectAtIndex:i];
Song *a2 =(Song *)[onAirSongs objectAtIndex:j];
if(a1.cellId == a2.cellId)
[hrSongs addObject:a2];
else{
[songsDict setObject:hrSongs forKey:[NSString stringWithFormat:@"%d",i]];
hrSongs = nil;
i=j-1;
break;
}
}//inner for
}//out for
[hrSongs release];
NSLog(@"songsdictionary count....%d",[songsDict count]);
}
答案 0 :(得分:3)
你将songsDict设置为nil。您无法将数组添加到不存在的字典中。
答案 1 :(得分:1)
blindJesse是正确的,但这里有更多问题:
您将songsDict
设置为nil。在Objective-C中,nil-eats-messages,因此,当你[songsDict setObject:.....]
时,你根本没有做任何事情。
在循环中间,您hrSongs = nil;
。通过循环的后续迭代将很乐意在{0}上调用addObject:
,什么都不做。
这整件事似乎有点奇怪; onAirSongs
包含什么?细胞?这似乎并没有真正保留任何模型 - 视图 - 控制器分离。 可能可以工作,但是如果您需要更改UI,那么您将面临严重的维护问题。