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

时间:2019-05-31 10:31:29

标签: core-data uicolor nskeyedarchiver

以下问题的答案已部分折旧: Storing UIColor object in Core Data Best way to save and retrieve UIColors to Core Data

1)iOS 12.0中不推荐使用'unarchiveObject(with :)':改为使用+ unarchivedObjectOfClass:fromData:error:

2)iOS 12.0中不推荐使用“ archivedData(withRootObject :)”:使用+ archivedDataWithRootObject:requiringSecureCoding:error:代替

 <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

   </android.support.v4.widget.NestedScrollView>

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

1 个答案:

答案 0 :(得分:0)

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)
     }
}