如何将PFObject链接到NSArray

时间:2016-01-17 20:52:31

标签: ios objective-c parse-platform nsarray pfobject

我有一个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;
}

1 个答案:

答案 0 :(得分:0)

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

目前,只需删除.lastObject,您就告诉系统foundUser类型为PFUser,并且您希望为其分配NSArray类型的值,而不是try { richTextBox1.Focus(); richTextBox1.Find(findwhat, findPos, RichTextBoxFinds.Reverse); richTextBox1.Select(findPos, findwhat.Length); findPos += findwhat.Length; } catch { findPos = 0; } 在一个强烈的世界里工作。