IOS中,有4种数据存储的方法:
1:NSKeyedArchiver与NSKeyedUnarchiver存档 http://www.jb51.cc/article/p-qayqvimm-mt.html
2:CoreData数据库 http://www.jb51.cc/article/p-uneagzdx-mt.html
3:File存储 http://www.jb51.cc/article/p-sahioxsn-bek.html
4:NSUserDefaults键值对,就是本文要介绍的.
使用方法非常简单:
let Defaults = NSUserDefaults.standardUserDefaults()
let boolKey = "boolKey"
let floatKey = "floatKey"
let doubleKey = "doubleKey"
let objectKey = "objectKey"
let intKey = "intKey"
//保存数据
func setData() {
Defaults.setBool(true,forKey: boolKey)
Defaults.setFloat(3.14159265358979323846,forKey: floatKey)
Defaults.setDouble(1.41414249812398467213441255598776,forKey: doubleKey)
Defaults.setObject("Json String ...",forKey: objectKey)
Defaults.setInteger(19991111,forKey: intKey)
}
//读取数据
func getData() {
print("\(Defaults.boolForKey(boolKey))")
print("\(Defaults.floatForKey(floatKey))")
print("\(Defaults.doubleForKey(doubleKey))")
print("\(Defaults.objectForKey(objectKey))")
print("\(Defaults.integerForKey(intKey))")
}
数据会保存在: (APP根目录)/Library/Preferences/(项目包名).plist 文件中
类似这样的:
至此: 文章就结束了,如有疑问: QQ群 Android:274306954 Swift:399799363 欢迎您的加入.