iOS开发——数据持久化Swift篇&(一)NSUserDefault
NSUserDefault
1 2 //******************** 5.1 NSUserDefault和对象归档 3 func useNSUserDefault() 4 { 5 //通过单利来创建一个NSUserDefaults对象,全局变量NSUserDefault,可在整个项目传递变量 6 var userDefault:NSUserDefaults = NSUserDefaults.standardUserDefaults() 7 8 //通过init方法创建 9 var userDefault1:NSUserDefaults = NSUserDefaults(suiteName: "SwiftClass")! 10 11 //获取userDefault单利下所有的值 12 println(userDefault.dictionaryRepresentation()) 13 14 //判断NSUserDefaults的“appMessage”key 在dictionaryRepresentation中是否存在,如果不存在就设置“appMessage”值为This is app message。 15 if(userDefault.objectForKey("message") == nil){ 16 userDefault.setObject("This_is_my_default_message", forKey: "message") 17 } 18 19 //如果想单独看某个key的设置,例如: 20 var dic = userDefault.dictionaryRepresentation() 21 var object_one:AnyObject? = (dic as NSDictionary).objectForKey("AppleKeyboards") 22 // //或者 23 // var object_one:AnyObject? = dic["AppleKeyboards"] 24 25 if let oValue: AnyObject! = object_one { 26 println(oValue) 27 } 28 29 30 //Int类型 31 //设置 32 userDefault.setInteger(123456, forKey: "Int") 33 //读取 34 var intValue = userDefault.integerForKey("Int") 35 println(intValue) 36 37 //Float类型 38 //设置 39 userDefault.setFloat(3.2, forKey: "Float") 40 //读取 41 var floatValue = userDefault.floatForKey("Float") 42 println(floatValue) 43 44 //Double类 45 //设置 46 userDefault.setDouble(5.6890, forKey: "Double") 47 //读取 48 var doubleValue = userDefault.doubleForKey("Double") 49 println(doubleValue) 50 51 //Bool类型 52 //设置 53 userDefault.setBool(true, forKey: "Bool") 54 //读取 55 var boolValue = userDefault.boolForKey("Bool") 56 println(boolValue) 57 58 //NSURL类型 59 //设置 60 userDefault.setURL(NSURL(string: "http://www.iphonetrain.com")!, forKey: "NSURL") 61 //读取 62 var urlValue = userDefault.URLForKey("NSURL") 63 println(urlValue) 64 65 66 67 //保存NSDate数据 68 //将对象转换成NSData流 69 var imageData:NSData = NSKeyedArchiver.archivedDataWithRootObject(UIImage(named: "SwiftClassWeiXin.png")!) 70 71 //存储NSData对象 72 userDefault.setObject(imageData, forKey: "imageData") 73 74 //读取数据 75 //获取NSData 76 var objData:AnyObject? = userDefault.objectForKey("imageData") 77 78 //还原对象 79 // var myImage:AnyObject? = NSKeyedUnarchiver.unarchiveObjectWithData(objData as NSData) 80 81 82 //2015年5月2号修改 83 var myImage:AnyObject? = NSKeyedUnarchiver.unarchiveObjectWithData(objData as! NSData) 84 85 86 87 //初始一个UIImage对象 88 println(myImage) 89 90 91 92 93 //自定义的类实现存取需要通过NSData做载体 94 95 //创建AppsModel的实例 96 var model = AppsModel(imageName: "appIcon2.png", app_Name: "租房点评", app_Description: "租房被骗?现在开始,你来改变这一切!《租房点评》为你而备,租房无忧!") 97 98 //实例对象转换成NSData 99 var modelData:NSData = NSKeyedArchiver.archivedDataWithRootObject(model) 100 101 //存储NSData对象 102 userDefault.setObject(modelData, forKey: "myAppModel") 103 104 105 106 107 108 109 110 111 //保存NSString, 112 userDefault.setValue("1_NSString", forKey: "NSString") 113 userDefault.setObject("1_NSString1", forKey: "NSString1") 114 115 //保存NSNumber, 116 var number:NSNumber = NSNumber(int: 32) 117 userDefault.setValue(number, forKey: "number") 118 userDefault.setObject(number, forKey: "number1") 119 120 //保存NSArray 121 var array1:NSArray = NSArray(array:["22222","33333"]) 122 userDefault.setValue(array1, forKey: "array") 123 userDefault.setObject(array1, forKey: "array1") 124 125 //保存NSDictionary 126 var dictionary:NSDictionary = NSDictionary(dictionary: ["1":"1111"]) 127 userDefault.setValue(dictionary, forKey: "dictionary") 128 userDefault.setObject(dictionary, forKey: "dictionary1") 129 130 131 132 133 var value:AnyObject? = userDefault.valueForKey("dictionary") 134 println(value) 135 136 value = userDefault.objectForKey("dictionary1") 137 println(value) 138 139 140 141 //-------- 删除所有的值 142 var ar:NSDictionary = userDefault.dictionaryRepresentation() 143 144 for key in ar.allKeys { 145 146 // userDefault.removeObjectForKey(key as String) 147 148 //2015年5月2号修改 149 userDefault.removeObjectForKey(key as! String) 150 151 userDefault.synchronize() 152 } 153 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。