Swift-如何存储数据在本地

使用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))"

    }

    

    required convenience init?(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)")

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值