使用 swift 5 从 Core Data 存储和检索 UIColor
Store and retrieve UIColor from Core Data using swift 5
以下问题的答案已部分折旧:Storing UIColor object in Core Data and Best way to save and retrieve UIColors to Core Data。
1) 'unarchiveObject(with:)' 在 iOS 12.0 中被弃用:改为使用 +unarchivedObjectOfClass:fromData:error:
2) 'archivedData(withRootObject:)' 在 iOS 12.0 中被弃用:改为使用 +archivedDataWithRootObject:requiringSecureCoding:error:
extension UIColor
{
class func color(withData data:Data) -> UIColor
{ return NSKeyedUnarchiver.unarchiveObject(with: data) as! UIColor }
func encode() -> Data
{ return NSKeyedArchiver.archivedData(withRootObject: self) }
}
尝试按照编译器说明和文档进行操作,我无法摆脱错误。有人可以澄清 Swift 5 中上述扩展方法的正确等效项吗?
为 Swift 5
尝试以下操作
extension UIColor {
class func color(data:Data) -> UIColor? {
return try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? UIColor
}
func encode() -> Data? {
return try? NSKeyedArchiver.archivedData(withRootObject: self, requiringSecureCoding: false)
}
}
以下问题的答案已部分折旧:Storing UIColor object in Core Data and Best way to save and retrieve UIColors to Core Data。
1) 'unarchiveObject(with:)' 在 iOS 12.0 中被弃用:改为使用 +unarchivedObjectOfClass:fromData:error:
2) 'archivedData(withRootObject:)' 在 iOS 12.0 中被弃用:改为使用 +archivedDataWithRootObject:requiringSecureCoding:error:
extension UIColor
{
class func color(withData data:Data) -> UIColor
{ return NSKeyedUnarchiver.unarchiveObject(with: data) as! UIColor }
func encode() -> Data
{ return NSKeyedArchiver.archivedData(withRootObject: self) }
}
尝试按照编译器说明和文档进行操作,我无法摆脱错误。有人可以澄清 Swift 5 中上述扩展方法的正确等效项吗?
为 Swift 5
尝试以下操作extension UIColor {
class func color(data:Data) -> UIColor? {
return try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? UIColor
}
func encode() -> Data? {
return try? NSKeyedArchiver.archivedData(withRootObject: self, requiringSecureCoding: false)
}
}