2015-04-23 32 views
0

我从我的Parse类返回一些对象到UITableView。从Parse.com返回对象时,setObjectForKey:object不能为零

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: object cannot be nil (key: $in) 

下面是我在做它:

@implementation MessageViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    [self refresh]; 

    _favorited = [[NSMutableArray alloc] initWithCapacity:1000]; 
} 

- (void)refresh { 

    NSArray *favorite_ids = [PFUser currentUser][@"favorites"]; 

    PFQuery *query = [PFQuery queryWithClassName:@"Messages"]; 
    [query whereKey:@"objectId" containedIn:favorite_ids]; 
    [query findObjectsInBackgroundWithBlock:^(NSArray *projects, NSError *error) { 
     if (error) { 
      NSLog(@"Error: %@", error.localizedDescription); 
      return; 
     } 

     PFQuery *query = [PFQuery queryWithClassName:@"MoreMessages"]; 
     [query whereKey:@"objectId" containedIn:favorite_ids]; 
     [query findObjectsInBackgroundWithBlock:^(NSArray *companies, NSError *error) { 
      if (error) { 
       NSLog(@"Error: %@", error.localizedDescription); 
       return; 
      } 

      [_favorited setArray:[projects arrayByAddingObjectsFromArray:companies]]; 
      NSLog(@"%@", _favorited); 
      [_refreshControl endRefreshing]; 
      [self.tableView reloadData]; 
      [SVProgressHUD dismiss]; 

     }]; 

    }]; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    if (_favorited.count < 0) { 
     return 1; 
    } 

    return self.favorited.count; 
} 

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (_favorited.count < 0) { 
     return 127; 
    } 
    return 65; 
} 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    if (_favorited.count < 0) { 
     NoFavoritesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NoFavoritesTableViewCell"]; 
     return cell; 
    } 

    MessagesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MessagesTableViewCell"]; 

    _favoritedObject = [_favorited objectAtIndex:indexPath.row]; 

    [(PFFile*)_favoritedObject[@"profilePic"] getDataInBackgroundWithBlock:^(NSData *data, NSError *error) { 
     cell.profilePic.image = [UIImage imageWithData:data]; 
    }]; 

    cell.name.text = _favoritedObject[@"name"]; 

    return cell; 
} 

@end 
+1

也许尝试调试代码,看看它死在什么行,什么变量是零当时? –

+0

尝试显示项目和公司阵列..公司越来越无我想..通过调试代码来检查它..! – Vidhyanand

+0

请添加一个异常断点,运行它并更新问题(或者仅在空列周围添加防御性代码)。如果不考虑这个或@PhillipMills的建议,这个问题就有可能在“为什么这段代码无法正常工作”的基础上结束。 – danh

回答

0

所有空解析值必须设置为[NSNull null],将接收到的,当我尝试打开查看,我得到这个错误作为[NSNull null]

如果是[NSNull null]或者不是[NSNull null],您应该检查每一个级别,这在许多级别上是不方便的。使用了两种方便的方法:将值分配给一个Parse对象时接收来自所述解析对象的值时

- (id)nsNullOrObject:(id)object 
{ 
    if(object == nil) 
    { 
     return [NSNull null]; 
    } 
    else 
    { 
     return object; 
    } 
} 
- (id)objectOrNil:(id)object 
{ 
    if(object == [NSNull null]) 
    { 
     return nil; 
    } 
    else 
    { 
     return object; 
    } 
} 

第一使用和第二。