应用程序在使用nsnull计数的循环的第二次运行时崩溃 [英] app is crashing on second run through of loop with nsnull count
本文介绍了应用程序在使用nsnull计数的循环的第二次运行时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个循环,在第一次循环中运行良好,但是在第二次循环中我得到了:
I have a loop that works fine the first time through but the second time through the loop I get:
-[NSNull count]: unrecognized selector sent to instance 0x3a094a70
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull count]: unrecognized selector sent to instance 0x3a094a70'
这是我的代码中崩溃的部分(最后一行):
Here is the part in my code where I know it is crashing (the last line):
...
NSLog(@"dict::%@",dictForPost);
// collect the photo urls in an array
photosInDict = [NSArray array];
// photos is an array of dictionaries in the dictionary
photosInDict = dictForPost[@"photos"];
if (photosInDict.count) {
....
我知道,当photosInDict在dic中没有图片时,它会崩溃,但是我不知道为什么,因为我启动了它上面的数组.
I know that when photosInDict doesnt have pictures in the dic it crashes but I dont get why since I initiated the array above it.
推荐答案
photosInDict = dictForPost[@"photos"]
替换先前分配并存储在photosInDict
中的对象.
因此,之前分配数组没有任何意义.只是
replaces the object previously allocated and stored in photosInDict
.
Therefore it does not make sense to allocate the array before. Just
NSArray * photosInDict = dictForPost[@"photos"];
然后检查
if ([photosInDict isKindOfClass:[NSArray class]]) {
// Yes, it is an array. Do something with it.
if ([photosInDict count]) {
...
}
}
这篇关于应用程序在使用nsnull计数的循环的第二次运行时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文