简体   繁体   English

xcode检查NSArray是否存在并包含对象?

[英]xcode check if an NSArray exist and contains objects?

I'm trying to get some app info using Apple API, that gives me an JSON file containing many objects. 我正在尝试使用Apple API获取一些应用信息,这给了我一个包含许多对象的JSON文件。

I tried to determine the type of the app (Universal/iPhone only/iPad only) like this 我尝试确定应用程序的类型(仅适用于Universal / iPhone / iPad)

if(([[appDetails objectForKey:@"screenshotUrls"] count]>0) && ([[appDetails objectForKey:@"ipadScreenshotUrls"] count]>0))
{
    cell.appDeviceLabel.text = @"Universal";
    cell.appDeviceLabel.backgroundColor = [UIColor colorWithRed:0.012 green:0.467 blue:0.784 alpha:1];
}

else if(([[appDetails objectForKey:@"screenshotUrls"] count]==0) && ([[appDetails objectForKey:@"ipadScreenshotUrls"] count]>0)) 
{
    cell.appDeviceLabel.text = @"iPad";
    cell.appDeviceLabel.backgroundColor = [UIColor colorWithRed:0.941 green:0.58 blue:0.016 alpha:1];        
}          
else if(([[appDetails objectForKey:@"screenshotUrls"] count]>0) && ([[appDetails objectForKey:@"ipadScreenshotUrls"] count]==0))
{
    cell.appDeviceLabel.text = @"iPhone";
    cell.appDeviceLabel.backgroundColor = [UIColor colorWithRed:0.016 green:0.459 blue:0.129 alpha:1];
}

Note : screenshotUrls is an array containing the images for the iphone version ipadScreenshotUrls is the one for iPad photos. 注意:screenshotUrls是一个包含iphone版本图像的数组ipadScreenshotUrls是用于iPad照片的图像。

I used the code above in my app and Apple accept it, but I get crash reports showing a problem at these lines. 我在我的应用程序中使用了上面的代码而Apple接受了它,但我收到崩溃报告,显示这些行存在问题。

May be because I'm testing the count of an array that is not found? 可能是因为我正在测试未找到的数组的计数? because if the app is iphone only for exemple, the array for iPad images won't exist. 因为如果应用程序只是iphone的例子,iPad图像的数组将不存在。 Any idea how can I solve this ? 知道怎么解决这个问题?

Thanks. 谢谢。

Does the JSON contains the value null for some key? JSON是否包含某些键的值null

If yes, this value is converted to NSNull in Obj-C and any method passed to this objects results in a crash. 如果是,则在Obj-C中将此值转换为NSNull ,并且传递给此对象的任何方法都会导致崩溃。 ( NSNull is different from nil in this respect.) (在这方面, NSNullnil不同。)

I usually encounter crashes with JSON in Obj-C for this reason very frequently. 由于这个原因,我经常在Obj-C中遇到使用JSON的崩溃。 You should put a check in place before using any value. 在使用任何值之前,您应该检查一下。

if (value == (typecast)[NSNull null]) {
    // use the value
}

Note that type casting is done only to avoid compiler warning. 请注意,类型转换仅用于避免编译器警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM