Ios setObjectForKey:从Parse.com返回对象时,对象不能为nil
我正在将许多对象从我的Parse类返回到UITableView。当我尝试打开视图时,出现以下错误:Ios setObjectForKey:从Parse.com返回对象时,对象不能为nil,ios,objective-c,uitableview,parse-platform,nsmutablearray,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)viewDidLoa
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
@implementation MessageViewController
-(无效)viewDidLoad{
[超级视图下载];
[自我刷新];
_favorited=[[NSMutableArray alloc]initWithCapacity:1000];
}
-(无效)刷新{
NSArray*收藏夹ID=[PFUser currentUser][@“收藏夹”];
PFQuery*query=[PFQuery queryWithClassName:@“消息”];
[查询whereKey:@“objectId”包含在:收藏夹ID中];
[查询findObjectsInBackgroundWithBlock:^(NSArray*项目,NSError*错误){
如果(错误){
NSLog(@“Error:%@”,Error.localizedDescription);
回来
}
PFQuery*query=[PFQuery queryWithClassName:@“MoreMessages”];
[查询whereKey:@“objectId”包含在:收藏夹ID中];
[查询findObjectsInBackgroundWithBlock:^(NSArray*公司,NSError*错误){
如果(错误){
NSLog(@“Error:%@”,Error.localizedDescription);
回来
}
[_FavoritedSetArray:[项目阵列添加对象阵列:公司];
NSLog(@“%@”,不受欢迎);
[_RefreshControlEndRefresh];
[self.tableView重载数据];
[SVD解散];
}];
}];
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
如果(_favorited.count<0){
返回1;
}
返回self.favorited.count;
}
-(CGFloat)tableView:(UITableView*)表视图行高度索引路径:(NSIndexPath*)索引路径{
如果(_favorited.count<0){
返回127;
}
返回65;
}
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
返回1;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
如果(_favorited.count<0){
NoFavoritesTableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:@“NoFavoritesTableViewCell”];
返回单元;
}
MessagesTableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:@“MessagesTableViewCell”];
_favoritedObject=[\u favorited objectAtIndex:indexPath.row];
[(PFFile*)_favoritedObject[@“profilePic”]GetDataInBackgroundithBlock:^(NSData*数据,NSError*错误){
cell.profilePic.image=[UIImage-imageWithData:data];
}];
cell.name.text=_favoritedObject[@“name”];
返回单元;
}
@结束
所有空解析值必须设置为[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;
}
}
第一个用于向解析对象分配值,第二个用于从解析对象接收值。也许可以尝试调试代码,看看它在哪一行上消失,而哪一个变量当时为零?尝试显示项目和公司数组。。我想公司是零。通过调试代码来检查它。。!请添加一个异常断点,运行它,并更新问题(或者只是在空列周围添加防御代码)。如果不采纳这个或@PhillipMills的建议,这个问题可以在“为什么这个代码不起作用”下结束。