0

我刚开始学习 Swift,所以这个问题的水平对你来说可能已经很明显了,但是,我根本不明白该怎么做。

我“创建”了一个简单的应用程序,它允许您添加一天的日志。每个单元格都存储您添加的时间、随时间变化的自定义图标以及日志文本本身(简单文本)。

一切正常。但是,由于我不知道“Userdefaults”的东西,所以每次我杀死应用程序时时钟都会重置。

我阅读了许多关于 Userdefaults 的文章,但我不知道如何做才能继续保存我的数据,即使我终止了应用程序。

这是我试图做的:

class ToDoItem: NSObject, NSCoding {


var title: String
var date: String
var type: String!

public init(title: String, date: String, type: String) {

    self.title = title
    self.date = date
    self.type = type

}

required init?(coder aDecoder: NSCoder)
{
    // Try to unserialize the "title" variable
    if let title = aDecoder.decodeObject(forKey: "title") as? String
    {
        self.title = title
        let dateFormatter = DateFormatter()
        dateFormatter.dateStyle = .none
        dateFormatter.timeStyle = .short
        self.date = dateFormatter.string(from: Date())

        let hour = NSCalendar.current.component(.hour, from: Date())
        var tempType = ""
        switch hour {
        case 5..<9: tempType = "morning_1"
        case 6..<12: tempType = "morning_2"
        case 12: tempType = "noon_1"
        case 13..<16: tempType = "afternoon_1"
        case 16..<20: tempType = "dusk_1"
        case 20..<23: tempType = "evening_1"
        case 23..<00: tempType = "midnight_1"
        default: tempType = "morning_1"


        }

        self.type = tempType

    }



    else
    {
        // There were no objects encoded with the key "title",
        // so that's an error.
        return nil
    }

    let userDefaults = UserDefaults.standard
    userDefaults.set(true, forKey: "title")

}

func encode(with aCoder: NSCoder)
{
    // Store the objects into the coder object
    aCoder.encode(self.title, forKey: "title")

    let defaults = UserDefaults.standard
    defaults.set(false, forKey: "title")


}

}

extension Collection where Iterator.Element == ToDoItem
{
    // Builds the persistence URL. This is a location inside
    // the "Application Support" directory for the App.
    private static func persistencePath() -> URL?
    {
        let url = try? FileManager.default.url(
            for: .applicationSupportDirectory,
            in: .userDomainMask,
            appropriateFor: nil,
            create: true)

        return url?.appendingPathComponent("todoitems.bin")
    }

    // Write the array to persistence
    func writeToPersistence() throws
    {
        if let url = Self.persistencePath(), let array = self as? NSArray
        {
            let data = NSKeyedArchiver.archivedData(withRootObject: array)
            try data.write(to: url)
        }
        else
        {
            throw NSError(domain: "com.example.MyToDo", code: 10, userInfo: nil)
        }
    }

    // Read the array from persistence
    static func readFromPersistence() throws -> [ToDoItem]
    {
        if let url = persistencePath(), let data = (try Data(contentsOf: url) as Data?)
        {
            if let array = NSKeyedUnarchiver.unarchiveObject(with: data) as? [ToDoItem]
            {
                return array
            }
            else
            {
                throw NSError(domain: "com.example.MyToDo", code: 11, userInfo: nil)
            }
        }
        else
        {
            throw NSError(domain: "com.example.MyToDo", code: 12, userInfo: nil)
        }
    }
}

谁能帮助我或至少指出我必须做什么?谢谢你!

4

1 回答 1

2

您正在使用NSKeyedArchiverNSKeyedUnarchiver机制与UserDefaults. 两者都是完全有效的,但你必须选择一个,它们不能一起工作。

在这里,您正在归档和取消归档ToDoItem. 为此,ToDoItem需要可存档,这意味着它必须实现NSCoding协议,即:

public protocol NSCoding {
    public func encode(with aCoder: NSCoder)
    public init?(coder aDecoder: NSCoder) // NS_DESIGNATED_INITIALIZER
}

您要保存的所有属性都必须添加到对象中/从NSCoder对象中提取。这是一个例子:

class ToDoItem: NSObject, NSCoding
{
    var title: String
    var date: Date

    required init?(coder aDecoder: NSCoder)
    {
        guard let title = aDecoder.decodeObject(forKey: "title") as? String else {
            return nil
        }
        guard let date = aDecoder.decodeObject(forKey: "date") as? Date else {
            return nil
        }
        self.title = title
        self.date = date
    }

    func encode(with aCoder: NSCoder)
    {
        aCoder.encode(self.title, forKey: "title")
        aCoder.encode(self.date, forKey: "date")
    }
}
于 2017-07-08T09:03:57.887 回答