Swift-如何存储数据在本地

来源:互联网 发布:淘宝决斗盘 编辑:程序博客网 时间:2024/06/07 03:20

使用playground来测试存储数据和读取数据, 使用的是访问本地目录。

//: Playground - noun: a place where people can play




import UIKit


import Foundation




//var str = "Hello, playground"


class Note: NSObject, NSCoding {

    

    let title : String

    

    let text : String

    

    let timestamp : Date

    

    init(title: String, text:String, timestamp: Date) {

        

        self.title = title

        

        self.text = text

        

        self.timestamp = timestamp

    }

    

    //    public func encode(with aCoder: NSCoder) {

    

    //        aCoder.encode(title, forKey: "title")

    

    //        aCoder.encode(text, forKey: "text")

    

    //        aCoder.encode(timestamp, forKey: "timestamp")

    

    //

    

    //    }

    

    struct PropertyKey {

        

        static let title ="title"

        

        static let text ="text"

        

        static let timestamp ="timestamp"

        

    }

    

    override var description:String {

        return"Note(title: \"\(title)\", text: \"\(text)\",timestamp:\(timestamp))"

    }

    

    requiredconvenienceinit?(coder aDecoder:NSCoder) // NS_DESIGNATED_INITIALIZER

        

    {

        

        guard let title = aDecoder.decodeObject(forKey:PropertyKey.title)as? String,

            

            let text = aDecoder.decodeObject(forKey:PropertyKey.text)as? String,

            

            let timestamp = aDecoder.decodeObject(forKey:PropertyKey.timestamp)as? Date else { return nil }

        

        self.init(title: title, text: text, timestamp: timestamp)

        

    }

    

    func encode(with aCoder: NSCoder) {

        

        aCoder.encode(title, forKey:PropertyKey.title)

        

        aCoder.encode(text, forKey:PropertyKey.text)

        

        aCoder.encode(timestamp, forKey:PropertyKey.timestamp)

        

    }

    

}



let note1 = Note(title:"A New Hope", text: "We have the plans.", timestamp: Date())

let note2 = Note(title:"A Dream", text: "We have a Dreem.", timestamp:Date())

let note3 = Note(title:"A Meeting", text: "We have two meetings.", timestamp: Date())


let notes = [note1,note2,note3]



//print(note)


//let archivedNote = NSKeyedArchiver.archivedData(withRootObject: note)

print(archivedNote)


//let unarchivedNote = NSKeyedUnarchiver.unarchiveObject(with: archivedNote) as? Note


let documentDirectory =FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!


print(documentDirectory)


let archiveURL = documentDirectory.appendingPathComponent("notes")

print(archiveURL)


let archivedNote =NSKeyedArchiver.archiveRootObject(notes, toFile:archiveURL.path)

let unarchivedNote = NSKeyedUnarchiver.unarchiveObject(withFile:archiveURL.pathas? [Note]


//print(unarchivedNote!.title)

//print(unarchivedNote!.text)

//print(unarchivedNote!.timestamp)


print(unarchivedNote![2].title)

for i in 0...2 {

    print("Title:\(unarchivedNote![i].title) and Text:\(unarchivedNote![i].text)  and Text:\(unarchivedNote![i].timestamp)")

}