Swift数据存储

2016-07-22  本文已影响74人  952625a28d0d
//MARK:获取沙盒路径
    func documentsDirectory() -> String{
        let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentationDirectory, NSSearchPathDomainMask.UserDomainMask, true)
        let documentDictionary:String = paths.first! as String
        return documentDictionary
    }
    
    //MARK:获取文件地址
    func dataFilePath() -> String{
        return documentsDirectory().stringByAppendingString("CheckList.plist")
    }```

- 对我们的数据模型进行编码解码 

```swift
 //MARK: 从Object解析出来
    init(coder aDecoder:NSCoder!){
        self.text = aDecoder.decodeObjectForKey("text") as! String
        self.checked = aDecoder.decodeObjectForKey("checked") as! Bool
    }
    
    //MARK: 编码成Object
    func encodeWithCoder(aCoder:NSCoder!) {
        aCoder.encodeObject(text, forKey: "text")
        aCoder.encodeObject(checked, forKey: "checked")
    }```

- 保存我们的数据

```swift
    func saveCheckListItems(){
        let data = NSMutableData()
        let archiver = NSKeyedArchiver(forWritingWithMutableData:data)
        archiver.encodeObject(arrData, forKey: "CheckItemList")
        archiver.finishEncoding()
        data.writeToFile(dataFilePath(), atomically: true)
    }```

![Paste_Image.png](https://img.haomeiwen.com/i189984/c1007e64cd3f69e9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

去文件夹中查看数据

![Paste_Image.png](https://img.haomeiwen.com/i189984/b3785c98ec13a4d4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

- 在数据发生变的时候存储数据

```swift
func addItem(item: MainItem) {
        let index = arrData.count
            arrData.append(item)
        let indexPath = NSIndexPath(forRow:index, inSection: 0)
        self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
        saveCheckListItems()
    }
    
    func editItem(item:MainItem){
        self.tableView.reloadData()
        saveCheckListItems()
    }```

- 应用初次加载的时候 读取本地数据

```swift
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #ffffff; min-height: 16.0px}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #ffffff}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #4cbf57}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #93c86a}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #c2349b}span.s3 {font-variant-ligatures: no-common-ligatures; color: #00afca}span.s4 {font-variant-ligatures: no-common-ligatures; color: #ffffff}span.s5 {font: 14.0px 'PingFang SC'; font-variant-ligatures: no-common-ligatures}span.s6 {font-variant-ligatures: no-common-ligatures; color: #e44448}span.s7 {font: 14.0px 'PingFang SC'; font-variant-ligatures: no-common-ligatures; color: #e44448}span.s8 {font-variant-ligatures: no-common-ligatures; color: #93c86a}

    override func viewDidLoad() {
        super.viewDidLoad()
        
//        self.onCreateData()
        
        //MARK: 读取数据
        loadLocationData()
        
        print("沙盒路径为\(documentsDirectory())")
        print("文件路径为\(dataFilePath())")
        
        saveCheckListItems()
    }```

![swift数据.gif](https://img.haomeiwen.com/i189984/9e8294334354149f.gif?imageMogr2/auto-orient/strip)
上一篇下一篇

猜你喜欢

热点阅读