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

Swift - 使用NSUserDefaults来进行本地数据存储,第1张

概述NSUserDefaults适合存储轻量级的本地客户端数据,比如记住密码功能,要保存一个系统的用户名、密码。使用NSUserDefaults是首选。下次再登陆的时候就可以直接从NSUserDefaults里面读取上次登陆的信息。 一般来说本地存储数据我们还可以是用SQlite数据库,或者使用自己建立的plist文件什么的,但这还得自己显示创建文件,读取文件,很麻烦,而是用NSUserDefault 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" ]) @[email protected](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">) } @H_907_403@}
总结

以上是内存溢出为你收集整理的Swift - 使用NSUserDefaults来进行本地数据存储全部内容,希望文章能够帮你解决Swift - 使用NSUserDefaults来进行本地数据存储所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1090801.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存