PFQuery没有获取对象ID

时间:2014-12-19 21:40:35

标签: xcode core-data swift parse-platform

以下代码检查Parse以查看是否已验证电子邮件地址。我能够从Core Data中检索对象ID(我将其保存在以前的视图控制器中)但是当涉及query.getObjectInBackgroundWithId(userID)时,userID由于某种原因nil。我收到错误:

错误:没有与查询匹配的结果(代码:101,版本:1.6.0)

import UIKit
import CoreData

class HomePage: UIViewController{

@IBOutlet var emailMessage: UILabel!

//var userID: String!

override func viewDidLoad() {
    super.viewDidLoad()

    var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)

    var context:NSManagedObjectContext = appDel.managedObjectContext!

    var request = NSFetchRequest(entityName: "Users")

    request.returnsObjectsAsFaults = false

    var results: NSArray = context.executeFetchRequest(request, error: nil)!

        var res = results [0] as NSManagedObject

        var userID = res.valueForKey("userID") as String

        println (userID) //Correct ID is retrieved here


    var query = PFQuery(className:"User")
    query.getObjectInBackgroundWithId(userID) { //ID becomes nil
        (email: PFObject!, error: NSError!) -> Void in
        if error == nil {

            let checkEmail = email["emailVerified"] as Bool

            if (checkEmail != true)
            {
                self.emailMessage.hidden = false
            }

            else
            {
                self.emailMessage.hidden = true
            }

        }
    }
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

2 个答案:

答案 0 :(得分:2)

我认为问题在于您为用户获取PFQuery的方式。您应该使用特殊用户查询:

var query = PFUser.query()

请参阅解析文档中的Users - Querying

答案 1 :(得分:2)

解析中的User表(以及所有其他解析管理的类)以下划线为前缀,因此您应该修复此行:

var query = PFQuery(className:"_User")