(first noticed on: Xcode 8.2.1, iOS 10, Swift 3) (still present as of: Xcode 9 beta 3, iOS11, Swift 4)
(首先注意到:Xcode 8.2.1,iOS 10,Swift 3)(仍然存在:Xcode 9 beta 3,iOS11,Swift 4)
We all know that the Core Data
concept of optionals
precedes and is not strictly tied to the Swift
concept of optionals
.
我们都知道,期权的核心数据概念先于并不严格依赖于Swift的期权概念。
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:
我们已经接受了即使Core Data属性被标记为Non-optional,自动生成的NSManagedObject子类也有一个可选类型:
(some people manually remove the ?
with no adverse effects, some don't, but that's beside the point)
(有些人手动删除?没有任何不利影响,有些人没有,但这不是重点)
(From here on the example and screenshots are for Bool
properties, but same goes for Int16/32/64
, Double
, Float
)
(从这里开始的示例和屏幕截图是针对Bool属性的,但同样适用于Int16 / 32/64,Double,Float)
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.
现在我注意到了相反的情况 - 当Bool类型的Core Data属性被标记为Optional(并且选择了Use Scalar Type,Xcode默认执行)时,自动生成的类具有非可选类型的变量。
Does this make sense? Is it a bug? Is the behaviour documented anywhere?
这有意义吗?这是一个错误吗?行为记录在任何地方吗?
And most importantly - how do I actually represent an optional Bool
?
最重要的是 - 我如何实际代表一个可选的Bool?
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
)
我可以想到一些解决方法,但它们似乎并不理想(例如,不使用标量,而是回到Bool的NSNumber表示。或者(甚至更糟)有一个叫做isVerified_isSet的单独的Bool)
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.
注意:我做了几个测试,如果默认值设置为None或NO,那么变量将保存为false(即使我从未在我的代码中实际分配它)。如果“默认值”设置为“是”,则变量将保存为true。尽管如此,这意味着(显然)没有办法在逻辑上将此变量表示为尚未设置。
2 个解决方案
#1
12
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.
我看到同样的事情,我认为这是一个错误。它没有记录在我能找到的任何地方。显然Core Data正在应用Objective-C样式假设,其中布尔值默认为NO,整数默认为0.核心数据/ Swift接口有一些粗糙的边缘,这是我之前没有考虑过的。
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.
这是一个很好的发现,但我认为你坚持使用它直到Apple解决它。你已经知道最好的解决方法,我同意这不是很好。我建议提交一个bug。
#2
5
This happens because Objective-C scalar types do not have a notion of nil value. Source: handling-core-data-optional-scalar-attributes
这是因为Objective-C标量类型没有nil值的概念。来源:handling-core-data-optional-scalar-attributes
#1
12
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.
我看到同样的事情,我认为这是一个错误。它没有记录在我能找到的任何地方。显然Core Data正在应用Objective-C样式假设,其中布尔值默认为NO,整数默认为0.核心数据/ Swift接口有一些粗糙的边缘,这是我之前没有考虑过的。
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.
这是一个很好的发现,但我认为你坚持使用它直到Apple解决它。你已经知道最好的解决方法,我同意这不是很好。我建议提交一个bug。
#2
5
This happens because Objective-C scalar types do not have a notion of nil value. Source: handling-core-data-optional-scalar-attributes
这是因为Objective-C标量类型没有nil值的概念。来源:handling-core-data-optional-scalar-attributes