存档和非存档数据:SWIFT 4

时间:2019-07-19 16:47:15

标签: ios swift core-data deprecated

我有以下代码。它已被弃用,并给了我黄色警告。我希望那消失。关于如何解决此问题有任何想法吗?

var linearScore = 0
var spreadScore = 0


func saveScores () {
    print("SAVE START")
    let scores : [Int] = [linearScore, spreadScore]
    let encodedData = NSKeyedArchiver.archivedData(withRootObject: scores)
    UserDefaults.standard.set(encodedData, forKey: "scores")
}

func retrieveScores () {
    print("Scores being retrieved")

    //comment out the if/let below to reset goals in dev

    if let data = UserDefaults.standard.data(forKey: "scores"),
        let scoreList = NSKeyedUnarchiver.unarchiveObject(with: data) as? [Int] {

        self.linearScore = scoreList[0]
        self.spreadScore = scoreList[1]

        self.updateTopScores()

    } else {
        print("There is an issue")
        self.saveScores()
    }
}

不建议使用的错误代码为以下两行:

let encodeData = NSKeyedArchiver.archivedData(withRootObject:scores)

让scoreList = NSKeyedUnarchiver.unarchiveObject(with:data)as? [Int] {

1 个答案:

答案 0 :(得分:2)

您将需要使用新的存档和非存档方法。首先,抛出您的方法,并使用以下方法进行归档:

class func archivedData(withRootObject object: Any, requiringSecureCoding requiresSecureCoding: Bool) throws -> Data

这是为了取消存档:

class func unarchiveTopLevelObjectWithData(_ data: Data) throws -> Any?

尝试这样:

func saveScores() throws {
    print(#function)
    let scores = [linearScore, spreadScore]
    let encodedData = try NSKeyedArchiver.archivedData(withRootObject: scores, requiringSecureCoding: false)
    UserDefaults.standard.set(encodedData, forKey: "scores")
}

func retrieveScores() throws {
    print(#function)
    if let data = UserDefaults.standard.data(forKey: "scores"),
        let scoreList = try  NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? [Int] {
    } else {
        print("There is an issue")
        self.saveScores()
    }
}

请注意,自Swift 4起,您也可以只使用Codable协议对模型数据进行编码/解码

var linearScore = 2
var spreadScore = 7

func saveScores() throws  {
    print(#function)
    let scores = [linearScore, spreadScore]
    let encodedData = try JSONEncoder().encode(scores)
    UserDefaults.standard.set(encodedData, forKey: "scores")
    print("scores saved")
}

func retrieveScored() throws {
    print(#function)
    if let data = UserDefaults.standard.data(forKey: "scores") {
        let scoreList = try JSONDecoder().decode([Int].self, from: data)
        print("scoreList:", scoreList)
    } else {
        print("No data found in user defaults for scores")
    }
}

如果您不想抛出错误,请根据需要使用do try catch:

func saveScores() {
    print(#function)
    let scores = [linearScore, spreadScore]
    do {
        try UserDefaults.standard.set(JSONEncoder().encode(scores), forKey: "scores")
        print("scores saved")
    } catch {
        print(error)
    }
}

func retrieveScores() {
    print(#function)
    if let data = UserDefaults.standard.data(forKey: "scores") {
         do {
            let scoreList = try JSONDecoder().decode([Int].self, from: data)
            print("scoreList:", scoreList)
        } catch {
            print(error)
        }

    } else {
        print("No data found in user defaults for scores")
    }
}