使用 swift 5 从 Core Data 存储和检索 UIColor

DrW*_*hat 1 core-data nskeyedarchiver uicolor

以下问题的答案已部分贬值:Storing UIColor object in Core DataBest way to save and检索 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) }
}
Run Code Online (Sandbox Code Playgroud)

尝试遵循编译器说明和文档,我无法消除错误。有人可以澄清 Swift 5 中上述扩展方法的正确等效项吗?

Ana*_*and 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)
     }
}
Run Code Online (Sandbox Code Playgroud)