У меня есть PFObject
, что я хотел бы равный массив, но он говорит несовместимый указатель.Как связать PFObject с NSArray
@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
я хочу, чтобы получить все объекты в массиве, но он говорит, что несовместимый указатель PFObject
к NSArray
.
Как я могу заставить его иметь весь объект, чтобы он отображал все объекты, а не последний объект?
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;
}
Мне нужен self.foundUser как PFUser, поскольку я использую его позже, чтобы добавлять и удалять друзей и выполнять другие запросы. есть ли способ, чтобы массив соответствовал PFUser без предупреждений? – Joey121
Ну, вы либо извлекаете элемент из массива (например, используя '.lastObject'), либо меняете свою логику, как я уже сказал. В любом случае поиск может возвращать несколько пользователей, как бы вы были уверены, что тот, который вы берете из массива, правильный? –
Я не могу быть уверен, так как мне нужно набрать почти точное имя пользователя, чтобы получить пользователя, я отредактирую свой вопрос выше, чтобы включить остальную часть моего кода, чтобы мы могли увидеть, как его изменить, чтобы по-прежнему вести себя одинаково. – Joey121