11

现在,使用 Swift 4 可以更轻松地在JSON 或 Properties list之间进行编码/解码。

但是我找不到如何使用 Codable 编码到数据,而不使用 Objective-C 方法initWithCoderencodeWithCoder.

考虑这个简单的类:

struct Question: Codable {
    var title: String
    var answer: Int
    var question: Int
}

如何使用CodingKeysand not initWithCoderand将其编码为 Data encodeWithCoder

编辑:我还需要能够使用 NSKeyedArchiver 反序列化以前保存在 userdefaults 中的对象。

4

4 回答 4

30

好吧,你不再需要NSKeyedArchiver.

尝试这个:

let questionObj = Question(title: "WWDC, 2017", answer: 1,question:1)
let encoder = JSONEncoder()
if let encoded = try? encoder.encode(questionObj) {
    UserDefaults.standard.set(encoded, forKey: "K_Question")
}
let decoder = JSONDecoder()
if let questionData = UserDefaults.standard.data(forKey: "K_Question"),
    let question = try? decoder.decode(Question.self, from: questionData) {
    print(question.title)
    print(question.answer)
    print(question.question)
}
于 2017-06-12T10:14:47.247 回答
6

Swift 5 : UserDefaults的一个非常简单的扩展:

extension UserDefaults {

    func save<T: Codable>(_ object: T, forKey key: String) {
        let encoder = JSONEncoder()
        if let encodedObject = try? encoder.encode(object) {
            UserDefaults.standard.set(encodedObject, forKey: key)
            UserDefaults.standard.synchronize()
        }
    }

    func getObject<T: Codable>(forKey key: String) -> T? {
        if let object = UserDefaults.standard.object(forKey: key) as? Data {
            let decoder = JSONDecoder()
            if let decodedObject = try? decoder.decode(T.self, from: object) {
                return decodedObject
            }
        }
        return nil
    }
}

用法

节省

UserDefaults.standard.save(currentUser, forKey: "currentUser")

得到

let user: User? = UserDefaults.standard.getObject(forKey: "currentUser")
于 2019-09-11T16:03:14.417 回答
0

那么它可以通过 JSONEncoder 和 JSONDecoder 来实现。

struct Question: Codable {
    var title: String
    var answer: Int
    var question: Int
}

let questionObj = Question(title: "Swift", answer: "Open Source",question:1)


let encoder = JSONEncoder()
if let encoded = try? encoder.encode(questionObj) {
    if let json = String(data: encoded, encoding: .utf8) {
        print(json)
    }

    let decoder = JSONDecoder()
    if let decoded = try? decoder.decode(Question.self, from: encoded) {
        print(decoded)
    }
}
于 2017-06-12T09:29:16.493 回答
0
struct Question: Codable {
    var title: String
    var answer: Int
    var question: Int
}

class UserDefaults_Question {
    static let key = "myapp.trick.question"

    static var value: UserDefaults_Question? {
        get {
            guard let data = UserDefaults.standard.data(forKey: key) else {
                print("no model for key: \(key)")
                return nil
            }
            guard let model = try? JSONDecoder().decode(UserDefaults_Question.self, from: data) else {
                print("failed to decode model for key: \(key)")
                return nil
            }
            print("did load model for key: \(key)")
            return model
        }
        set {
            guard let value = newValue, let data: Data = try? JSONEncoder().encode(value) else {
                print("removing model for key: \(key)")
                UserDefaults.standard.removeObject(forKey: key)
                return
            }
            print("inserting model for key: \(key)")
            UserDefaults.standard.set(data, forKey: key)
        }
    }
}

UserDefaults_Question.value = Question(title: "Next President", answer: 666, question: -1)
于 2018-12-04T11:59:09.950 回答