DrWhat
DrWhat

Reputation: 2490

Store and retrieve UIColor from Core Data using swift 5

The answers to the following questions are partially depreciated: Storing UIColor object in Core Data and Best way to save and retrieve UIColors to Core Data.

1) 'unarchiveObject(with:)' was deprecated in iOS 12.0: Use +unarchivedObjectOfClass:fromData:error: instead

2) 'archivedData(withRootObject:)' was deprecated in iOS 12.0: Use +archivedDataWithRootObject:requiringSecureCoding:error: instead

extension UIColor 
{
   class func color(withData data:Data) -> UIColor 
   { return NSKeyedUnarchiver.unarchiveObject(with: data) as! UIColor }

   func encode() -> Data 
   { return NSKeyedArchiver.archivedData(withRootObject: self) }
}

Trying to follow the compiler instructions and documentation, I can't get rid of errors. Could someone please clarify the correct equivalent for the above extension methods in Swift 5?

Upvotes: 1

Views: 1341

Answers (1)

Anand
Anand

Reputation: 5332

Try the following for 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)
     }
}

Upvotes: 5

Related Questions