我在从解析查询中获取的对象中的指针中获取数据时出现问题。我在查询中使用include键,但是在返回的对象数组中获取包含键信息时遇到问题。我创建了一个for循环来迭代结果,但不知道如何将它们存储回数组中。这是我的代码:
- (void) loadUsersPFUserIsFollowing
{
//Retrieving PFUser Curren Users followers and storing in Array
PFQuery *queryFollowingCount = [PFQuery queryWithClassName:@"Activity"];
[queryFollowingCount whereKey:@"type" equalTo:@"follow"];
[queryFollowingCount whereKey:@"fromUser" equalTo:[PFUser currentUser]];
//Access the the user following informatioin
[queryFollowingCount includeKey:@"toUser"];
//[queryFollowingCount setCachePolicy:kPFCachePolicyCacheThenNetwork];
[queryFollowingCount findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
[HUD hide:YES];
if (!error) {
searchResultsArray = [[NSArray alloc] initWithArray:objects];
NSLog(@"%@", objects);
for (PFObject * postObject in objects) {
PFObject *postAuthor = [postObject objectForKey:@"toUser"][@"username"];
NSLog(@"retrieved related Post Author: %@", postAuthor);
}
[self.tableView reloadData];
} else {
NSLog(@"Something went wrong");
NSLog(@"%@", error);
}
}];
}
答案 0 :(得分:2)
for (PFObject * postObject in objects) {
PFObject *postAuthor = [postObject objectForKey:@"toUser"][@"username"];
NSLog(@"retrieved related Post Author: %@", postAuthor);
}
默认情况下,在获取对象时,不会获取相关的PFObject。这些物品'在获取值之前,无法检索这些值:
for (PFObject * postObject in objects) {
//Im assuming in your code, postObject is either nil or it's there but it's values are nil except its objectId. We need to fetch this object.
[postObject fetchIfNeededInBackgroundWithBlock:^(PFObject *object, NSError *error){
PFObject *postAuthor = [object objectForKey:@"toUser"][@"username"];
NSLog(@"retrieved related Post Author: %@", postAuthor);
[self.tableView reloadData];
}];
}