如何 link PFObject 到 NSArray

How to link PFObject to an NSArray

我有一个 PFObject 我想等于一个数组,但它说指针不兼容。

@property (nonatomic, strong) PFUser *foundUser;

PFQuery *query = [PFUser query];
        [query whereKey:@"username" containsString:searchText];
        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            if (!error) {
                if (objects.count > 0) {

                    self.foundUser = objects.lastObject;

                } 

我不希望这一行 self.foundUser = objects.lastObject;.lastObject 我想获取数组中的所有对象,但它说 PFObjectNSArray 的不兼容指针.

如何让它拥有所有对象,以便它显示所有对象而不是最后一个对象?

编辑:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    // Return the number of sections.
    if (self.foundUser) {

        return 1;

    } else {

        return 0;
    }

}

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

    // Return the number of rows in the section.
    if (self.foundUser) {

        return 1;

    } else {

        return 0;

    }

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
                                                            forIndexPath:indexPath];

    PFUser *user = self.foundUser;
    if (self.foundUser) {

        //set the label as the foundUsers username
        cell.textLabel.text = self.foundUser.username;

    }

        if ([self isFriend:self.foundUser]) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        } else {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    PFUser *user = self.foundUser; 
    PFRelation *friendsRelation = [self.currentUser relationForKey:@"friendsRelation"];

    if ([self isFriend:user]) {
        cell.accessoryType = UITableViewCellAccessoryNone;

        for (PFUser *friend in self.friends) {
            if ([friend.objectId isEqualToString:user.objectId]) {
                [self.friends removeObject:friend];
                break;
            }
        }
                [friendsRelation removeObject:user];

        } else {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            [self.friends addObject:user];
            [friendsRelation addObject:user];

    }

    [self.currentUser saveInBackgroundWithBlock:^(BOOL succeed, NSError *error) {

        if (error) {
            NSLog(@"error %@ %@", error, [error userInfo]);
        }

    }];

}

#pragma mark - helper methods

-(BOOL)isFriend:(PFUser *)user
{
    for (PFUser *friend in self.friends) {
        if ([friend.objectId isEqualToString:user.objectId]) {
            return YES;
        }
    }

    return NO;
}

将 foundUser 的类型更改为 NSArray,然后将行更改为 self.foundUser = objects(将 foundUser 重命名为 foundUsers 可能会使其更清楚)。

目前,当简单地删除 .lastObject 时,您是在告诉系统 foundUser 是 PFUser 类型,并且您希望为其分配一个 NSArray 类型的值,但它不会在 strongly-typed 世界工作。