加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift - 使用NSUserDefaults来进行本地数据存储

发布时间:2020-12-14 02:36:52 所属栏目:百科 来源:网络整理
导读:NSUserDefaults适合存储轻量级的本地客户端数据,比如记住密码功能,要保存一个系统的用户名、密码。使用NSUserDefaults是首
NSUserDefaults适合存储轻量级的本地客户端数据,比如记住密码功能,要保存一个系统的用户名、密码。使用NSUserDefaults是首选。下次再登陆的时候就可以直接从NSUserDefaults里面读取上次登陆的信息。

一般来说本地存储数据我们还可以是用SQlite数据库,或者使用自己建立的plist文件什么的,但这还得自己显示创建文件,读取文件,很麻烦,而是用NSUserDefaults则不用管这些东西,就像读字符串一样,直接读取就可以了。
NSUserDefaults支持的数据格式也很多,有:Int,Float,Double,BOOL,甚至AnyObject类型。
1,下面通过一个样例演示NSUserDefaults的用法:
(1)如果是第一次运行程序通过CFUUIDCreate方法生成一个唯一字符串作为用户id储存起来(形如:B8DDB58D-73BF-4E39-A051-365858FC4626)
(2)往后运行时直接从NSUserDefaults中把用户id取出
1
2
3
4
5
6
7
8
9
10
11
12
class func get_uuid() -> String {
var userid = NSUserDefaults .standardUserDefaults().stringForKey( "hangge" )
if (userid != nil ){
return userid!
} else {
uuid_ref = CFUUIDCreate ( )
uuid_string_ref = CFUUIDCreateString ,uuid_ref)
uuid: = NSString (format: uuid_string_ref)
.standardUserDefaults().setObject(uuid,forKey: )
uuid
}
}

2,对原生数据类型的储存和读取
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
userDefault = .standardUserDefaults()
//AnyObject
userDefault.setObject( "hangge.com" "Object" )
objectValue: AnyObject ? = userDefault.objectForKey( )
//Int类型
userDefault.setInteger(12345,monospace!important; min-height:inherit!important; color:blue!important">"Int" )
intValue = userDefault.integerForKey(
//Float类型
userDefault.setFloat(3.2,monospace!important; min-height:inherit!important; color:blue!important">"Float" )
floatValue = userDefault.floatForKey( )
//Double类型
userDefault.setDouble(5.2240,monospace!important; min-height:inherit!important; color:blue!important">"Double" )
doubleValue = userDefault.doubleForKey( )
//Bool类型
userDefault.setBool( true "Bool" )
boolValue = userDefault.boolForKey( )
//NSURL类型
userDefault.setURL( NSURL (string: "http://hangge.com" )!,monospace!important; min-height:inherit!important; color:blue!important">"NSURL" )
urlValue = userDefault. URLForKey ( )
//NSString类型
"NSString" )
nsStringValue = userDefault.objectForKey( ) as ! NSString
//NSNumber类型
number: NSNumber (int:22)
userDefault.setObject(number,monospace!important; min-height:inherit!important; color:blue!important">"NSNumber" )
number = userDefault.objectForKey( NSNumber
//NSArray类型
array: NSArray (array: [ "123" "456" ])
userDefault.setObject(array,monospace!important; min-height:inherit!important; color:blue!important">"NSArray" )
//NSDictionaryy类型
dictionary: NSDictionary (dictionary: [ "1" : ])
userDefault.setObject(dictionary,monospace!important; min-height:inherit!important; color:blue!important">"NSDictionary" )
dictionary = userDefault.objectForKey( NSDictionary

3,系统对象的存储与读取
系统对象实现存储,需要通过archivedDataWithRootObject方法转换成NSData为载体,才可以存储。下面以UIImage对象为例:
15
//UIImage对象存储
//将对象转换成NSData流
image = UIImage (named: "apple.png" imageData: NSData NSKeyedArchiver .archivedDataWithRootObject(image!)
//存储NSData对象
userDefault.setObject(imageData,monospace!important; min-height:inherit!important; color:blue!important">"imageData"
//UIImage对象读取
//获取NSData
objData: = userDefault.objectForKey( NSData
//还原对象
myImage = NSKeyedUnarchiver .unarchiveObjectWithData(objData) UIImage
println (myImage)

4,自定义对象的存储和读取
如果想要存储自己定义的类,首先需要对该类实现NSCoding协议来进行归档和反归档(序列化和反序列化)。即该类内添加func encodeWithCoder(_encoder:NSCoder)方法和init(coder decoder:NSCoder)方法,将属性进行转换。
38
//自定义对象存储
model = UserInfo (name: "航歌" "3525" //实例对象转换成NSData
modelData: .archivedDataWithRootObject(model)
userDefault.setObject(modelData,monospace!important; min-height:inherit!important; color:blue!important">"myModel" //自定义对象读取
myModelData = userDefault.objectForKey( NSData
myModel = .unarchiveObjectWithData(myModelData) UserInfo
//----- 自定义对象类 -----
class : NSObject {
name: String
phone: String
//构造方法
init (name: = "" ){
self .name = name
.phone = phone
super . ()
}
//从nsobject解析回来
(coder aDecoder: NSCoder !){
.name=aDecoder.decodeObjectForKey( "Name" ! String
.phone=aDecoder.decodeObjectForKey( "Phone" String
}
//编码成object
encodeWithCoder(aCoder: !){
aCoder.encodeObject(name,monospace!important; min-height:inherit!important">)
aCoder.encodeObject(phone,monospace!important; min-height:inherit!important">)
}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读