How to represent Core Data optional Scalars (Bool/Int/Double/Float) in Swift?

前端 未结 4 1607
你的背包
你的背包 2020-12-09 07:13

(first noticed on: Xcode 8.2.1, iOS 10, Swift 3) (still present as of: Xcode 9 beta 3, iOS11, Swift 4)

We all know that the Core Data concept of o

4条回答
  •  眼角桃花
    2020-12-09 07:57

    I could use Objective-C types to manage these cases. In these cases, for scalars types, you can use NSNumber.

     @NSManaged public var myDouble: NSNumber?
    

    In the model myDouble is an optional double with nil value by default.

    To get the real value you only need to use:

    myEntity.myDouble?.doubleValue
    

提交回复
热议问题