下面通过一个例子将联系人数据保存到沙盒的“documents”目录中。(联系人是一个数组集合,内部为自定义对象)。
功能如下:
1,点击“保存”将联系人存入userList.plist文件中
2,点击“读取”从数据文件中加载解析出联系人
注意:
1,本例使用了NSCoder,这个封装了许多技术细节,使用它我们可以很轻易的将对象写到文件中,也可以用它将文件中的对象转换回来。
2,自定义对象必须添加如下两个方法,这个才能顺利的被序列化编码存储和读取。
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" ) } } |