我收到了一些回复JSON,并且工作正常,但我需要检查一些null值,
我找到了不同的答案,但似乎还没有工作,
NSArray *productIdList = [packItemDictionary objectForKey:@"ProductIdList"];
Run Code Online (Sandbox Code Playgroud)
我试过了
if ( !productIdList.count ) //which breaks the app,
if ( productIdList == [NSNull null] ) // warning: comparison of distinct pointer types (NSArray and NSNull)
Run Code Online (Sandbox Code Playgroud)
那么发生了什么?如何修复此问题并null在我的阵列中检查?
谢谢!
rob*_*off 32
使用强制转换消除警告:
if (productIdList == (id)[NSNull null])
Run Code Online (Sandbox Code Playgroud)
如果productIdList实际上是[NSNull null],那么做productIdList.count会引发异常,因为NSNull不理解该count消息.
您还可以使用方法检查对象的类isKindOfClass:.
例如,在您的情况下,您可以执行以下操作:
if ([productIdList isKindOfClass:[NSArray class]])
{
// value is valid
}
Run Code Online (Sandbox Code Playgroud)
或(如果您确定NSNull表示无效值)
if([productIdList isKindOfClass:[NSNull class]])
{
// value is invalid
}
Run Code Online (Sandbox Code Playgroud)
您可以使用isEqual选择器:
if ( [productIdList isEqual:[NSNull null]] )
Run Code Online (Sandbox Code Playgroud)