一般来说本地存储数据我们还可以是用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,对原生数据类型的储存和读取
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对象为例:
//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)方法,将属性进行转换。
//自定义对象存储 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来进行本地数据存储所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)