setObjectForKey:从Parse.com返回对象时,对象不能为nil

时间:2015-04-23 12:15:26

标签: ios objective-c uitableview parse-platform nsmutablearray

我从Parse类返回一些对象到UITableView。我尝试打开视图时收到此错误:

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 :(得分:0)

所有空分析值必须设置为[NSNull null],并将作为[NSNull null]收到。

在许多级别上都会带来不便,因为如果是[NSNull null],则应检查每个级别。我使用了两种方便的方法:

- (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;
    }
}

第一个用于为Parse对象赋值,第二个用于从Parse对象接收值时使用。