//联系人:石虎 QQ: 1224614774昵称:嗡嘛呢叭咪哄
iOS保存数据有很多种方式,这里提供一种使用字典保存数据的方法
直接上代码
-
- let path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0].stringByAppendingPathComponent("contacts.data");
- NSLog("Path:%@", path);
-
-
- var arr = NSMutableDictionary(contentsOfFile: path);
-
-
- let index = arc4random();
- var k = String(index);
- var v = "abc - "+String(index)
-
-
- arr?.setObject(v, forKey: k);
-
-
- var ok = arr?.writeToFile(path, atomically: true)
- NSLog("Write:%@",ok!)
-
-
- if(arr != nil){
- for item in arr!.keyEnumerator() {
- let s : String = arr?.valueForKey(item as! String) as! String;
- NSLog("k:%@ v:%@", item as! String, s);
- }
- }