将值设置为nil - swift / parse

时间:2015-02-26 19:10:35

标签: ios swift parse-platform null

我的应用程序中有一个UISwitch,我想注册并取消注册推送通知。我目前正在使用解析,我希望应用程序也在服务器上的usertable中更新到devicetoken。我正在使用此代码但是当我翻转开关时它会崩溃。 "不能在PFObject上使用nil作为键或值。"

那么,如果我不能设置为nil,我将如何处理从当前安装中删除值并使用它。

 UIApplication.sharedApplication().unregisterForRemoteNotifications()
 switchValue.setOn(false, animated: true)
 installation.deviceToken = nil
 user["devicetoken"] = nil
 user.saveInBackground()

2 个答案:

答案 0 :(得分:4)

你应该在PFObject上使用removeObjectForKey。这样,当您从解析中获取相同的数据时,您将获得nil,而不是空字符串

答案 1 :(得分:0)

只需使用user["devicetoken"] = "",并确保在尝试使用令牌发送通知之前检查空字符串。