encodeObject(forKey :)和decodeObjectForKey(_ :)有什么区别

时间:2018-10-11 21:23:58

标签: swift nscoding

这里有两个使用的例子。

// The name is required. If we cannot decode a name string, the initializer should fail.
guard let name = aDecoder.decodeObject(forKey: PropertyKey.name) as? String else {
    os_log("Unable to decode the name for a Meal object.", log: OSLog.default, type: .debug)
    return nil
}

// Because photo is an optional property of Meal, just use conditional cast.
let photo = aDecoder.decodeObjectForKey(PropertyKey.photo) as? UIImage

1 个答案:

答案 0 :(得分:1)

NSKeyedUnarchiver最初是一个桥接目标c类,因此您可以使用

decodeObjectForKey(_:)迅速<3.0

decodeObject(forKey:)迅速> = 3

在内部,它们是同一件事,语法在不断变化,请看一下Thread