This is the object I created with primary key username and the error when I try to run the add object code.
class Login_item: Object {
dynamic var username = String()
dynamic var password = String()
override static func primaryKey() -> String? {
return "username"
}
}
This is the code that add new object and update object.
func save_login_info() {
let new_login_entry = Login_item()
new_login_entry.username = Username.text!
new_login_entry.password = Password.text!
let realm = try! Realm()
try! realm.write {
realm.add(new_login_entry)
print("Login info save as: \(Realm.Configuration.defaultConfiguration.fileURL!)")
}
try! realm.write {
realm.add(new_login_entry, update: true)
}
}
Error I got when execute the code.
fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=10 "Migration is required due to the following errors: - Property 'username' has been made a primary key." UserInfo={NSLocalizedDescription=Migration is required due to the following errors: - Property 'username' has been made a primary key., Error Code=10}: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-703.0.18.8/src/swift/stdlib/public/core/ErrorType.swift, line 54