为什么Xcode生成的NSManagedObject子类总是包含可选属性,即使它们被标记为非可选属性? [英] Why Xcode generated NSManagedObject Subclass contains always optional properties even if they are marked as non-optional?

查看:111
本文介绍了为什么Xcode生成的NSManagedObject子类总是包含可选属性,即使它们被标记为非可选属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Film的核心数据实体,并具有属性标题和日期。我注意到生成的NSManagedObject子类生成可选的NSManaged属性,即使我在核心数据检查器中标记为非可选。

I have a core data entity named Film and has a properties title and date. I noticed that the generated NSManagedObject subclass generate optional NSManaged property even if I marked like non optional in core data inspector.

Core Data inspector

NSManagedObject子类

我可以手动将其更改为非可选属性,或者是更好的选择将其作为可选属性?为什么?

I can manually change it as non-optional property or is a better choice to left it as optional? Why?

推荐答案

可选是指与Core Data不同的内容,而不是Swift。

"Optional" means something different to Core Data than it does to Swift.


  • 如果Core Data属性不是可选的,则在保存更改时必须具有非零值。在其他时候,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 know or 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属性为非可选属性并不意味着迅速的感觉的术语。这就是为什么生成代码使这些属性是可选的 - 就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屋!

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