保存当前用户数据Realm ios

时间:2018-05-25 15:26:34

标签: ios swift realm

无法将当前用户数据保存到Realm数据库。 这是我的代码

@IBAction func createUser(_ sender: Any) {

    let commomRealm =  try! Realm(configuration: commonRealmConfig(user: SyncUser.current!))
    userRecord = realm?.objects(User.self).filter(NSPredicate(format: "userID = %@", myIdentity!)).first

    userRecord?.setValue(self.nameTxt!.text, forKey: "name")
    userRecord?.setValue(self.surnameTxt!.text, forKey: "surname")
    userRecord?.setValue(self.emailTxt!.text, forKey: "email")
    userRecord?.setValue(self.numberTxt!.text, forKey: "phoneNumber")

    try! self.realm?.write {
        //  newUser = realm.create(User.self, value: ["userID": myIdentity])
        commomRealm.add(userRecord!, update: true)
    }
}

这是我的用户类 -

class User: Object {

    @objc dynamic var userID = ""
    @objc dynamic var name : String = ""
    @objc dynamic var surname: String = ""
    @objc dynamic var passcode: String = ""
    @objc dynamic var dateOfBirth: String = "" // or String if we not calculating age for customer
    @objc dynamic var address: String = ""
    @objc dynamic var email: String = ""
    @objc dynamic var phoneNumber: String = "" // or Int
    @objc dynamic var accountBalance: Float = 0.0

    override static func primaryKey() -> String? {
        return "userID"
    }
}

0 个答案:

没有答案