ios - 从解析错误中检索空数组

标签 ios xcode cocoa-touch parse-platform nsarray

在我的应用程序中,用户可以查看自己的统计信息。我像这样从 Parse 中检索一个数组:

...
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    userArray = [objects valueForKey:@"myArray"];
}]; 

当数组实际包含某些内容时,代码工作得很好......但是如果在 Parse.com 中没有任何内容保存到数组中,我会出错......

例子: 我运行此代码进行测试:

NSLog(@"%lu", (unsigned long)[[userArray objectAtIndex:0] count]);

当数组包含某些内容时,它会记录正确的数字(有效)。但是当数组为空时,我得到这个错误:

"-[NSNull count]: unrecognized selector sent to instance 0x199084fe0"

我不知道发生了什么事..任何人都知道发生了什么事吗?

最佳答案

您得到的不是空数组,而是 NSNull。您可以阅读更多关于差异的信息 here .

NSNull 的测试很简单。考虑用 nil 替换不是 NSArrays 的值。

id myArray = userArray.firstObject;
myArray = [myArray isKindOfClass:[NSArray class]] ? myArray : nil;
NSLog(@"%i", [myArray count]); //will log 0 because myArray is nil

关于ios - 从解析错误中检索空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26108706/

相关文章:

ios - 如何在提交到 App Store 之前检测私有(private) api 的使用情况以避免拒绝

iphone - Objective-C for iPhone - 使用简单的日期命令时应用程序崩溃

ios - 使用 UserDefaults 保存图像数组

ios - 如何在 OSX 10.10 Yosemite 上安装 xcode 5

xcode - Cocoa - 以编程方式转到前台/后台

ios - xcode 8 swift 3 latlong 在模拟器上工作但不在设备中工作

iOS 为 initWithNibName 生成的函数结构

ios - 使用 Xcode Asset Catalogs 进行 UIImage 缓存

ios - 我正在使用 Notification Service Extension,但我无法在通知中获取图像,如何在通知中心获取图像?

ios - Firebase 中的登录和注销问题