Difference between UserDefaults() and UserDefaults.standard

久未见 提交于 2020-01-03 21:09:42

问题


Is there a difference between UserDefaults() and UserDefaults.standard in Swift 3.0?


回答1:


UserDefaults - Gives you a new object, each object is allocated a different memory and deallocated when object scope is finished.

UserDefaults.standard - Gives you the singleton object by using the class method standard the object received by this method is allocated single memory throughout the application.

And the usage of them if you´re interesedted in that:

// Set
UserDefaults.standard.set("YOUR STRING", forKey: "key")
UserDefaults().set("YOUR STRING", forKey: "key")


// Get
UserDefaults.standard.string(forKey: "key")
UserDefaults().string(forKey: "key")


来源:https://stackoverflow.com/questions/40026196/difference-between-userdefaults-and-userdefaults-standard

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