微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Swift - 使用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)
@H_502[email protected]().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 = @H_502[email protected]()
//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" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas, "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: @H_502_128@= userDefault.objectForKey( NSData
//还原对象
myImage = NSKeyedUnarchiver .unarchiveObjectWithData(objData) UIImage
println (myImage)

4,自定义对象的存储和读取
如果想要存储自己定义的类,首先需要对该类实现NSCoding协议来进行归档和反归档(序列化和反序列化)。即该类内添加func encodeWithCoder(_encoder:NSCoder)方法和init(coder decoder:NSCoder)方法,将属性进行转换。
38
//自定义对象存储
model = UserInfo (name: "航歌" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,phone: "3525" //实例对象转换成NSData
modelData: @H_502[email protected](model)
userDefault.setobject(modelData,monospace!important; min-height:inherit!important; color:blue!important">"myModel" //自定义对象读取
myModelData = userDefault.objectForKey( NSData
myModel = @H_502[email protected](myModelData) UserInfo
//----- 自定义对象类 -----
class @H_502_128@: NSObject {
name: String
phone: String
//构造方法
init (name: = "" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,phone: ){
self .name = name
.phone = phone
super . ()
}
//从nsobject解析回来
(coder aDecoder: NSCoder !){
.name=aDecoder.decodeObjectForKey( "Name" ! String
.phone=aDecoder.decodeObjectForKey( "Phone" String
}
//编码成object
encodeWithCoder(aCoder: @H_502_128@!){
aCoder.encodeObject(name,monospace!important; min-height:inherit!important">)
aCoder.encodeObject(phone,monospace!important; min-height:inherit!important">)
}
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐