Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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.

enter image description here

enter image description here

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

like image 522
Davide Avatar asked Nov 05 '15 15:11

Davide


People also ask

How to create NSManagedObject subclass in xcode 10?

From the Xcode menu bar, choose Editor > Create NSManagedObject Subclass. Select your data model, then the appropriate entity, and choose where to save the files. Xcode places both a class and a properties file into your project.

How do I get NSManagedObject?

Still inside the Core Data editor, go to the Editor menu and choose Create NSManagedObject Subclass. Make sure your data model is selected then click Next. Make sure the Commit entity is checked then click Next again.

What is scalar type core data?

Scalar Types Core Data has support for many common data types like integers, floats, booleans, and so on. However, by default, the data model editor generates these attributes as NSNumber properties in the managed object subclasses.


2 Answers

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

  • 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.

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.

like image 72
Tom Harrington Avatar answered Oct 21 '22 13:10

Tom Harrington


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.

like image 21
MirekE Avatar answered Oct 21 '22 13:10

MirekE