标签:
下面通过一个例子将联系人数据保存到沙盒的“documents”目录中。(联系人是一个数组集合,内部为自定义对象)。
1
2
3
4
5
6
7
8
9
10
11
|
//从nsobject解析回来 init (coder aDecoder: NSCoder !){ self .name=aDecoder.decodeObjectForKey( "Name" ) as String self .phone=aDecoder.decodeObjectForKey( "Phone" ) as String } //编码成object func encodeWithCoder(aCoder: NSCoder !){ aCoder.encodeObject(name,forKey: "Name" ) aCoder.encodeObject(phone,forKey: "Phone" ) } |
--- ViewController.swift ---
1
2
3
4
5
6
7
8
9
10
11
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
|
import UIKit class ViewController : UIViewController { var dataModel = DataModel () override func viewDidLoad() { super .viewDidLoad() // Do any additional setup after loading the view, typically from a nib. //初始化模拟数据 onCreateData() } //创建模拟数据 func onCreateData(){ dataModel.userList.append( UserInfo (name: "张三" , phone: "1234" )) dataModel.userList.append( UserInfo (name: "李四" , phone: "1212" )) dataModel.userList.append( UserInfo (name: "航歌" , phone: "3525" )) } //保存数据 @IBAction func saveData(sender: AnyObject ) { dataModel.saveData() } //读取数据 @IBAction func loadData(sender: AnyObject ) { dataModel.loadData() } override func didReceiveMemoryWarning() { super .didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } |
--- DataModel.swift ---
1
2
3
4
5
6
7
8
9
10
11
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
import UIKit class DataModel : NSObject { var userList = [ UserInfo ]() override init (){ super . init () println ( "沙盒文件夹路径:\(documentsDirectory())" ) println ( "数据文件路径:\(dataFilePath())" ) } //保存数据 func saveData() { var data = NSMutableData () //申明一个归档处理对象 var archiver = NSKeyedArchiver (forWritingWithMutableData: data) //将lists以对应Checklist关键字进行编码 archiver.encodeObject(userList, forKey: "userList" ) //编码结束 archiver.finishEncoding() //数据写入 data.writeToFile(dataFilePath(), atomically: true ) } //读取数据 func loadData() { //获取本地数据文件地址 let path = self .dataFilePath() //声明文件管理器 let defaultManager = NSFileManager () //通过文件地址判断数据文件是否存在 if defaultManager.fileExistsAtPath(path) { //读取文件数据 let data = NSData (contentsOfFile: path) //解码器 let unarchiver = NSKeyedUnarchiver (forReadingWithData: data!) //通过归档时设置的关键字Checklist还原lists userList = unarchiver.decodeObjectForKey( "userList" ) as Array //结束解码 unarchiver.finishDecoding() } } //获取沙盒文件夹路径 func documentsDirectory()-> String { var paths = NSSearchPathForDirectoriesInDomains ( NSSearchPathDirectory . DocumentationDirectory , NSSearchPathDomainMask . UserDomainMask , true ) var documentsDirectory: String = paths.first as String return documentsDirectory } //获取数据文件地址 func dataFilePath ()-> String { return self .documentsDirectory().stringByAppendingString( "userList.plist" ) } } |
--- UserInfo.swift ---
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
import UIKit class UserInfo : NSObject { var name: String var phone: String //构造方法 init (name: String = "" ,phone: String = "" ){ self .name = name self .phone = phone super . init () } //从nsobject解析回来 init (coder aDecoder: NSCoder !){ self .name=aDecoder.decodeObjectForKey( "Name" ) as ! String self .phone=aDecoder.decodeObjectForKey( "Phone" ) as ! String } //编码成object func encodeWithCoder(aCoder: NSCoder !){ aCoder.encodeObject(name,forKey: "Name" ) aCoder.encodeObject(phone,forKey: "Phone" ) } } |
Swift - 本地数据的保存与加载(使用NSCoder将对象保存到.plist文件)
标签:
原文地址:http://www.cnblogs.com/Free-Thinker/p/4841063.html