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

IosXcodeSwiftCore Data

Ios Problem Overview


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?

Ios Solutions


Solution 1 - Ios

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

Solution 2 - Ios

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.

Solution 3 - Ios

Create managedobject class and change the entity class type to manual and add these classes to your project scope.

Edit your managedObject to make them non-optional. 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.

Solution 4 - Ios

The Optional checkbox in the data model inspector has nothing to do with Swift optionals. 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. Suppose you have a description attribute that's a string. If you select the Optional checkbox you could leave the description blank and still save the entity.

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavideView Question on Stackoverflow
Solution 1 - IosTom HarringtonView Answer on Stackoverflow
Solution 2 - IosMirekEView Answer on Stackoverflow
Solution 3 - IosmeghaView Answer on Stackoverflow
Solution 4 - IosMohamed AbdelraZekView Answer on Stackoverflow