iosobjective-cparse-platformnsarraypfobject

How to link PFObject to an NSArray


I have a PFObject that I would like to equal an array but it says incompatible pointer.

@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;

                } 

I don't want this line self.foundUser = objects.lastObject; to say .lastObject I want to get all objects in the array but it says incompatible pointer of PFObject to NSArray.

How can I get it to have all object so it shows all objects and not last object?

EDIT:

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

Solution

  • Change the type of foundUser to NSArray and then change the line to self.foundUser = objects (rename foundUser to foundUsers maybe to make it more clear).

    Currently, when simply removing .lastObject, you're telling the system that foundUser is of type PFUser and you want to assign it a value of type NSArray which does not work in a strongly-typed world.