我尝试使用realm
从swift3
删除对象。当用户使用google登录时,userinfos将存储在realm db中。现在我想删除用户,当我点击一个按钮。
这是我未完成的代码:
@IBAction func deleteUser(_ sender: Any) {
let realm = try! Realm()
let userObject = realm.objects(userInfos.self).filter("id == 0").first
realm.delete(userObject)
try! realm.write
print("deleted")
print(userObject)
}
上面的代码不起作用。有什么问题?
感谢您的帮助!
答案 0 :(得分:0)
尝试:
let realm = try! Realm()
if let userObject = realm.objects(userInfos.self).filter("id == 0").first {
print("User is existed, it's being deleted.")
try! realm.write {
realm.delete(userObject)
}
print("Deleted.")
}
else{
print("User is not found.")
}