如何在Swift中表示Core Data可选标量(Bool / Int / Double / Float)? [英] How to represent Core Data optional Scalars (Bool/Int/Double/Float) in Swift?

查看:80
本文介绍了如何在Swift中表示Core Data可选标量(Bool / Int / Double / Float)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(首先注意到:Xcode 8.2.1,iOS 10,Swift 3)
(至今仍存在:Xcode 9 beta 3,iOS11,Swift 4)

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

我们都知道可选项目核心数据概念是在先的,并不严格地与 Swift 可选项目的概念。

We all know that the Core Data concept of optionals precedes and is not strictly tied to the Swift concept of optionals.

如果核心数据属性标记为非可选,则自动生成的 NSManagedObject 子类具有可选类型:

And we have accepted that even if a Core Data attribute is marked as Non-optional, the auto-generated NSManagedObject subclass has an optional type:


(有些人手动删除了却没有不良影响,有些没有,但是那不是重点)

(some people manually remove the ? with no adverse effects, some don't, but that's beside the point)

(从此处的示例开始,屏幕截图是针对 Bool 属性的,但对于 Int16 / 32/64 也是如此, Double Float

(From here on the example and screenshots are for Bool properties, but same goes for Int16/32/64, Double, Float)

现在我注意到了[R相反-当类型为 Bool Core Data 属性标记为 Optional (并且选择了使用标量类型,默认情况下Xcode会这样做),自动生成的类的变量为非可选类型。

Now I noticed the reverse - when a Core Data attribute of type Bool is marked as Optional (and Use Scalar Type is chosen, which Xcode does by default), the auto-generated class has a variable of a non-optional type.

这有意义吗?是虫子吗?

Does this make sense? Is it a bug? Is the behaviour documented anywhere?

最重要的是-我实际上如何表示可选的 Bool

And most importantly - how do I actually represent an optional Bool?

我可以想到一些解决方法,但它们似乎并不理想(例如,不使用标量,而是回到 NSNumber 表示 Bool ,或者(更糟糕的是)有一个单独的 Bool 称为 isVerified_isSet

I can think of some work-arounds, but they don't seem ideal (e.g. not using scalars, but going back to NSNumber representation of the Bool. Or (even worse) having a separate Bool called something like isVerified_isSet)

注意:我再进行了几次测试,如果默认值设置为,则该变量将保存为 false (即使我从未在代码中实际分配它)。如果默认值设置为 YES ,则变量将另存为 true 。尽管如此,这仍然意味着(显然)尚无逻辑表示该变量尚未设置。

Note: I did a couple more tests and if the Default Value is set to None or to NO, then the variable gets saved as false (even if I never actually assign it in my code). If the Default Value is set to YES, then the variable gets saved as true. Still, this means that (apparently) there is no way to logically represent this variable as not having been set yet.

推荐答案

I看到同一件事,我认为这是一个错误。我找不到任何地方的文档。显然,Core Data在这里采用了Objective-C风格的假设,其中布尔默认值为 NO ,而整数默认值为0。CoreData / Swift接口具有一些粗糙的边缘,这是我以前从未考虑过的。

I see the same thing, and I consider it to be a bug. It's not documented anywhere that I can find. Apparently Core Data is applying Objective-C style assumptions here, where a boolean defaults to NO, and an integer defaults to 0. The Core Data/Swift interface has some rough edges, and this is one I hadn't considered before.

这是一个很好的发现,但我认为您会坚持使用,直到Apple解决为止。您已经知道最好的解决方法,但我认为这不是很好。我建议提交错误。

It's a good find but I think you're stuck with it until Apple addresses it. You already know the best workarounds, which I agree aren't great. I recommend filing a bug.

这篇关于如何在Swift中表示Core Data可选标量(Bool / Int / Double / Float)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆