iOS老司机请飘过, Swift新手学习基础。
类似于Android的SharedPreferences, iOS提供类UserDefaults存储少量的键值对数据到文件里。 手机或程序重启后文件仍然存在。 iOS各进程间不能互相访问。
UserDefaults类的注释里说明了支持的数据类型:
Key-Value Store: NSUserDefaults stores Property List objects (NSString, NSData, NSNumber, NSDate, NSArray, and NSDictionary) identified by NSString keys, similar to an NSMutableDictionary.
为了编码键唯一,建议使用枚举作为key。
//使用枚举作为关键字, 避免重复键值
enum Keys: String {
case Array = "array"
case StrName = "name"
case IntAge = "age"
}