Ios 阅读';空';关于UITableView

Ios 阅读';空';关于UITableView,ios,objective-c,uitableview,null,nsjsonserialization,Ios,Objective C,Uitableview,Null,Nsjsonserialization,所以我使用了JSON序列化,并且一直在使用 NSString *referencestr = [[notificationarray objectAtIndex:indexPath.row] valueForKey:@"Reference"]; 它可以很好地获取数据并将其显示在表视图中。但是,当数据为空时,我的表崩溃,我的应用程序关闭。我应该如何解决这个问题,或者将NULL读取为nil 我假设您的通知数组是通过调用[NSJSONSerialization JSONObjectWithData:

所以我使用了JSON序列化,并且一直在使用

NSString *referencestr = [[notificationarray objectAtIndex:indexPath.row] valueForKey:@"Reference"];

它可以很好地获取数据并将其显示在表视图中。但是,当数据为空时,我的表崩溃,我的应用程序关闭。我应该如何解决这个问题,或者将NULL读取为nil

我假设您的
通知数组是通过调用
[NSJSONSerialization JSONObjectWithData:options:encoding:][/code>创建的。如果正在解析的JSON中存在
null
对象,那么该调用将愉快地为您提供一个包含
NSNull
实例的数组

您的客户机代码应该对不合作的数据提供程序具有健壮性。您应该检查
[notificationarray objectAtIndex:indexPath.row]
的类。如果您希望对象是
NSDictionary
,则可以这样检查:

id theObject = [notificationarray objectAtIndex:indexPath.row];
if ([theObject isKindOfClass:[NSDictionary class]]) {
    // Work under the assumption that theObject is a dictionary.
} else {
    // Log an error, bail out, etc.  Whatever's the appropriate
    // response for malformed data.
}

在这种情况下,您可以检查该值是否为null。为此,请进行一次检查

  if ([[notificationarray objectAtIndex:indexPath.row]valueForKey:@"Reference"]!=[NSNull null]) {
       lblDescription.text=[[notificationarray objectAtIndex:indexPath.row]valueForKey:@"Reference"];
   }
  else
   {
     //any value you wants to display in label
   }

您是否尝试过检查NSNull?可能与NSNull重复,请澄清您所说的
NULL
是什么意思。你真正拥有什么?你可以通过responsetoselector调用检查你的结果,以确保它支持特定调用。