解析:查找查询中找到的对象数

时间:2015-05-21 06:19:35

标签: ios objective-c parse-platform

如何查找查询中找到的对象数量?以下代码始终打印" 0",但在数据库中有一个用户具有该用户名。

PFQuery *query = [PFQuery queryWithClassName:@"User"];
    [query whereKey:@"username" equalTo:self.usernameField.text];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if ([objects count] == 0) {
            NSLog(@"error %lu", (unsigned long)[objects count]);
        }
        else {
            NSLog(@"no error");
        }
    }];

我做错了什么?

2 个答案:

答案 0 :(得分:1)

应使用_User类的+query方法对PFUser类执行查询

PFQuery *userQuery = [PFUser query];   //Note the difference here.
[userQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)    {
   if ([objects count] == 0) {
        NSLog(@"error %lu", (unsigned long)[objects count]);
    }
    else {
        NSLog(@"no error");
    }
}];

答案 1 :(得分:0)

试试这个,因为解析默认用户实体是以“_User”

启动的
PFQuery *query = [PFQuery queryWithClassName:@"_User"];

[query whereKey:@"username" equalTo:self.usernameField.text];

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)    {

   if ([objects count] == 0) {
        NSLog(@"error %lu", (unsigned long)[objects count]);
    }
    else {
        NSLog(@"no error");
    }
}];