UserDefaults.standard set nil Error in XCode 8 and Swift 3

对着背影说爱祢 提交于 2019-12-10 10:19:35

问题


This seems a bug. I am trying to remove an existing value in the defaults.

UserDefaults.standard.set(nil, forKey: "test-me")
let val = UserDefaults.standard.object(forKey: "test-me")
print ("val=\(val)")

I got the following output -

val=Optional(<62706c69 73743030 d4010203 04050608 09582476 65727369 6f6e5824 6f626a65 63747359 24617263 68697665 72542474 6f701200 0186a0a1 0755246e 756c6c5f 100f4e53 4b657965 64417263 68697665 72d10a0b 54726f6f 74800008 111a232d 3237393f 51545900 00000000 00010100 00000000 00000c00 00000000 00000000 00000000 00005b>)

I am running in XCode 8 / iOS 10 / iPhone 7 simulator.


回答1:


The comment from Rob in the other post looks correct. Setting the value to "nil" will save it as NSData. To remove the key, try this instead:

UserDefaults.standard.removeObject(forKey: "test-date")

You will then probably get back "nil" when doing object(forKey: "test-date")



来源:https://stackoverflow.com/questions/39518680/userdefaults-standard-set-nil-error-in-xcode-8-and-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!