Xcode NSManagedObject 子类在标记为非可选时包含可选 [英] Xcode NSManagedObject subclass contains optionals when they are marked as non-optional
问题描述
我有一个名为 Film 的核心数据实体,它具有属性 title 和 date.我注意到生成的 NSManagedObject 子类包含可选的 NSManaged 属性,即使我在核心数据检查器中将这些属性标记为非可选.
I have a core data entity named Film which has properties title and date. I noticed that the generated NSManagedObject subclass contains optional NSManaged properties even though I marked the properties as non optional in the core data inspector.
我可以手动将其更改为非可选属性还是将其保留为可选属性是更好的选择?为什么?
Can I can manually change it as non-optional property or is it a better choice to leave it as optional? Why?
推荐答案
Optional"对 Core Data 的意义不同于对 Swift 的意义.
"Optional" means something different to Core Data than it does to Swift.
- 如果 Core Data 属性不是可选的,则它必须具有非 nil 值保存更改时.在其他时候,Core Data 并不关心该属性是否为 nil.
- 如果 Swift 属性不是可选的,则它必须在初始化完成后始终具有非 nil 值.
- If a Core Data attribute is not optional, it must have a non-nil value when you save changes. At other times Core Data doesn't care if the attribute is nil.
- If a Swift property is not optional, it must have a non-nil value at all times after initialization is complete.
将 Core Data 属性设为非可选并不意味着它在 Swift 术语中是非可选的.这就是为什么生成的代码使这些属性成为可选的——就 Core Data 而言,除非保存更改,否则具有 nil 值是合法的.
Making a Core Data attribute non-optional does not imply that it's non-optional in the Swift sense of the term. That's why generated code makes these properties optional-- as far as Core Data is concerned, it's legal to have nil values except when saving changes.
这篇关于Xcode NSManagedObject 子类在标记为非可选时包含可选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!