NSInvalidArgumentException:应用程序在打开时崩溃

NSInvalidArgumentException: App crashes upon opening

我正在尝试构建一个应用程序,用户可以在其中对帖子点赞和发表评论,这很像 instagram。所有帖子/喜欢/评论都保存在 parse.com!我已经能够将代码放在一起,它应该像在示例应用程序上一样工作,但是当我移动文件时,这段代码使它崩溃:

 [query whereKey:@"user" containedIn:self.usersToDisplay];

崩溃信息是:

由于未捕获的异常而终止应用程序 'NSInvalidArgumentException',原因:'* setObjectForKey:对象不能为 nil(键:$in)'**

其余代码如下所示:

- (void)viewDidLoad
{
// self.usersToDisplay = @[[PFUser currentUser]];

self.parseClassName = @"Photo";
[super viewDidLoad];

self.tableView.layer.cornerRadius = 10;
self.tableView.layer.masksToBounds = YES;
}

- (void)setUsersToDisplay:(NSArray *)usersToDisplay {
_usersToDisplay = usersToDisplay;

if (self.isViewLoaded)
{
    [self loadObjects];
}
}


  - (PFQuery *)queryForTable {
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];

// If Pull To Refresh is enabled, query against the network by default.
if (self.pullToRefreshEnabled) {
    query.cachePolicy = kPFCachePolicyNetworkOnly;
}

 [query whereKey:@"user" containedIn:self.usersToDisplay];
[query addDescendingOrder:@"createdAt"];
return query;
}

 -(PFTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
  {
   PhotoViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"ImageCellReuseID"];
if(!cell)
{
    cell = [[PhotoViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"ImageCellReuseID"];
}

cell.imageView.file = object[@"image"];
[cell.imageView loadInBackground];

NSDateFormatter* formatter = [NSDateFormatter new];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateStyle:NSDateFormatterShortStyle];
NSString* uploaded = [formatter stringFromDate:object.createdAt];

cell.textLabel.text = [NSString stringWithFormat:@"Taken By %@ ", object[@"username"]];

cell.detailTextLabel.text = uploaded;

return cell;
}


  -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  [self performSegueWithIdentifier:@"ShowDetail" sender:indexPath];
 }

知道为什么会崩溃吗?

您的数组 self.usersToDisplay 中的某个值似乎没有有效的查询对象。

来自解析文档:

If you want to retrieve objects matching several different values, you can use whereKey:containedIn:, providing an array of acceptable values. This is often useful to replace multiple queries with a single query. For example, if you want to retrieve scores made by any player in a particular list:

// Finds scores from any of Jonathan, Dario, or Shawn
// Using PFQuery
NSArray *names = @[@"Jonathan Walsh", @"Dario Wunsch", @"Shawn Simon"]; 
[query whereKey:@"playerName" containedIn:names];

确保数组中的所有值都与数据库中的值匹配。

干杯!