我正在xCode Beta的swift 2.0中构建一个IOS应用程序。我构建了一个自定义用户类,其子类为PFUser,该类的相关摘录在下面再现
import UIKit
class User: PFUser, Entity {
// MARK: - Declare Field Variables
@NSManaged var firstName : String
@NSManaged var lastName : String
//Several other state variables follow in the same format
///initializing a new person
init (firstName: String, lastName: String, password: String, email: String) {
super.init()
super.username = email
super.password = password
super.email = email
//set initial key-value pairs for person
self.firstName = firstName
self.lastName = lastName
// Special method to sign up new user
super.signUp()
}
// Additional methods placed here
// MARK: - Conformation to PFObjectSubclassing
override class func initialize() {
self.registerSubclass()
}
// MARK: - Conformation to Entity
func saveToDatabase() {
//Saves the new PFObject Asyncronously
self.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
if (success) { print("User \(self.username) succesfully saved to database.") }
else { print(error) }
}
}
}
使用解析GUI,我验证用户已成功保存到数据库。
我正在尝试使用Appdelegate中编写的类检索数据,该类从Appdelegate调用,如下所示:
var examplePerson: User!
func funcThatIsCalled() {
var query : PFQuery = PFUser.query()!
var people = query.findObjects()
self.examplePerson = people![0] as! User
}
这将返回错误:
“无法将'PFUser'类型的值(0x108326490)转换为'MyPersonalApp.User'(0x108323f80)。”
将User.registerSublass()添加到Appdelegate,如下所示:
User.registerSubclass()
// Initialize Parse.
Parse.setApplicationId("myAppID",
clientKey: "MyClientKey")
导致错误:
由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'在使用Parse之前,必须使用registerSubclass注册类PFUser。'
答案 0 :(得分:0)
想出来:
PFUser.query()
应该是
User.query()