I need a way to save key and value pairs in swift for my application. I am new to IOS programming, but I know Android and I am looking for something like shared preferences in android, but for swift. By "save," I mean that when the user quits out of the application, the key and value pairs are saved in the local data.
我需要一种方法来保存swift中的键和值对我的应用程序。我是IOS编程的新手,但我知道Android,我正在寻找像android中的共享首选项,但对于swift。通过“保存”,我的意思是当用户退出应用程序时,键和值对将保存在本地数据中。
2 个解决方案
#1
10
Swift 3.0
Swift 3.0
Saving Data:
保存数据:
let userDefaults = UserDefaults.standard
userDefaults.set(yourKey, forKey: "yourKey")
userDefaults.synchronize()
Reading Data:
阅读数据:
if let yourVariable: AnyObject = UserDefaults.standard.object(forKey: "yourKey") as AnyObject? { }
#2
6
What you are looking for is called "Userdefaults" - you can use them like this:
您正在寻找的是“Userdefaults” - 您可以像这样使用它们:
Saving Data:
保存数据:
let userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setObject(variable, forKey: "yourKey")
userDefaults.synchronize()
Reading Data:
阅读数据:
if let yourVariable: AnyObject = NSUserDefaults.standardUserDefaults().objectForKey("yourKey") {
....
Here you find more information:
在这里您可以找到更多信息
https://developer.apple.com/library/prerelease/ios/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/index.html
#1
10
Swift 3.0
Swift 3.0
Saving Data:
保存数据:
let userDefaults = UserDefaults.standard
userDefaults.set(yourKey, forKey: "yourKey")
userDefaults.synchronize()
Reading Data:
阅读数据:
if let yourVariable: AnyObject = UserDefaults.standard.object(forKey: "yourKey") as AnyObject? { }
#2
6
What you are looking for is called "Userdefaults" - you can use them like this:
您正在寻找的是“Userdefaults” - 您可以像这样使用它们:
Saving Data:
保存数据:
let userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setObject(variable, forKey: "yourKey")
userDefaults.synchronize()
Reading Data:
阅读数据:
if let yourVariable: AnyObject = NSUserDefaults.standardUserDefaults().objectForKey("yourKey") {
....
Here you find more information:
在这里您可以找到更多信息
https://developer.apple.com/library/prerelease/ios/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/index.html