简体   繁体   English

Xcode NSManagedObject 子类在标记为非可选时包含可选

[英]Xcode NSManagedObject subclass contains optionals when they are marked as non-optional

I have a core data entity named Film which has properties title and date.我有一个名为 Film 的核心数据实体,它具有属性标题和日期。 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.我注意到生成的 NSManagedObject 子类包含可选的 NSManaged 属性,即使我在核心数据检查器中将这些属性标记为非可选。

在此处输入图片说明

在此处输入图片说明

Can I can manually change it as non-optional property or is it a better choice to leave it as optional?我可以手动将其更改为非可选属性还是将其保留为可选属性是更好的选择? Why?为什么?

"Optional" means something different to Core Data than it does to Swift. “可选”对 Core Data 的意义不同于对 Swift 的意义。

  • If a Core Data attribute is not optional, it must have a non-nil value when you save changes .如果 Core Data 属性不是可选的,则在保存更改时它必须具有非 nil 值。 At other times Core Data doesn't care if the attribute is nil.在其他时候,Core Data 并不关心该属性是否为 nil。
  • If a Swift property is not optional, it must have a non-nil value at all times after initialization is complete.如果斯威夫特属性是不可选的,初始化完成之后,它必须在任何时候都非零值。

Making a Core Data attribute non-optional does not imply that it's non-optional in the Swift sense of the term.将 Core Data 属性设为非可选并不意味着它在 Swift 术语中是非可选的。 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.这就是为什么生成的代码使这些属性成为可选的——就 Core Data 而言,拥有 nil 值是合法的,除非保存更改。

This is a known issue.这是一个已知的问题。 Some people change it to non-optional with no adverse effects, I keep it the way it was generated and hope for early fix.有些人将其更改为非可选且没有不利影响,我保持其生成方式并希望早日修复。

It always helps if you submit a bug to Apple to increase visibility and priority.如果您向 Apple 提交错误以提高可见性和优先级,这总是有帮助的。

Create managedobject class and change the entity class type to manual and add these classes to your project scope.创建 managedobject 类并将实体类类型更改为手动并将这些类添加到您的项目范围。

Edit your managedObject to make them non-optional.编辑您的 managedObject 使它们成为非可选的。 This means you need to maintain this class yourself and do any changes both in the core data model and the class这意味着您需要自己维护这个类并在核心数据模型和类中进行任何更改

If your data model is stable and won't be changed then you can use this.如果您的数据模型稳定且不会更改,那么您可以使用它。

The Optional checkbox in the data model inspector has nothing to do with Swift optionals.数据模型检查器中的 Optional 复选框与 Swift 选项无关。 The checkbox determines whether or not the attribute is required to have a value.该复选框确定是否需要属性具有值。

If you deselect the Optional checkbox for an attribute, you must give that attribute a value or you will get an error when saving.如果取消选择属性的可选复选框,则必须为该属性指定一个值,否则保存时会出错。 By selecting the Optional checkbox you can save without giving the attribute a value.通过选择 Optional 复选框,您可以在不给属性值的情况下进行保存。 Suppose you have a description attribute that's a string.假设您有一个字符串的 description 属性。 If you select the Optional checkbox you could leave the description blank and still save the entity.如果您选择了 Optional 复选框,您可以将描述留空并仍然保存实体。

Here's another example.这是另一个例子。 Suppose you have text fields to let a person enter their home, work, and cell phone numbers.假设您有文本字段,让一个人输入他们的家庭、工作和手机号码。 These phone numbers should be optional attributes.这些电话号码应该是可选属性。 You wouldn't want to require someone to have a home phone number, a work phone number, and a cell phone number just to save the person's data.您不希望仅仅为了保存此人的数据而要求某人拥有家庭电话号码、工作电话号码和手机号码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM