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

Parse: Find number of objects found in query

如何找到在查询中找到的对象数?以下代码始终打印“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");
        }
    }];

我做错了什么?

试试这个,因为解析默认用户实体是用“_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");
    }
}];

_User class 执行查询应该使用 PFUser class

+query 方法来完成
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");
    }
}];