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

查看:13
本文介绍了如何在 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)

我们都知道 optionalsCore Data 概念先于 optionals.

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

并且我们已经接受,即使 Core Data 属性被标记为 Non-optional,自动生成的 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/64Double、<代码>浮动)

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

现在我注意到了相反的情况 - 当 Bool 类型的 Core Data 属性被标记为 Optional(并且 Use Scalar Type 被选中,Xcode 默认这样做),自动生成的类有一个 non-optional 类型的变量.

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?

我可以想到一些变通办法,但它们似乎并不理想(例如,不使用标量,而是回到 BoolNSNumber 表示.或者(甚至更糟)有一个单独的 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)

注意:我做了几个测试,如果 Default Value 设置为 NoneNO,那么变量会被保存作为 false (即使我从未在我的代码中实际分配它).如果 Default Value 设置为 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.

推荐答案

我看到了同样的事情,我认为这是一个错误.在我能找到的任何地方都没有记录.显然 Core Data 在这里应用了 Objective-C 风格的假设,其中布尔值默认为 NO,整数默认为 0.Core Data/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天全站免登陆