2
votes

I'm making a purely Swift project, and when I create an entity in model file, then use Editor->Create NSManagedObject Subclass to create class file for the entity, in the model, the Class property for entity becomes PRODUCT_MODULE_NAME.entityName, this will cause core data to fail loading NSManagedObject subclass instance.

I know how to get pass by this by using @objc() and rename the class property in model file, but is there any better idea?

1
I have the feeling that the "Create NSManagedObject Subclass ..." in Xcode is still broken ... Maybe it should be ${PRODUCT_MODULE_NAME}.entityName and should be expanded automatically. Does that work? - Martin R
I don't think that you need @objc() and a name change in the model file - I think it is one or the other. The fundamental problem us Swift's namespaces - either convert the entities to objective C or prefix their names with the module name. - Ali Beadle

1 Answers

1
votes

Two options:

  1. Replace the PRODUCT_MODULE_NAME with the value of this build setting. By default, it will be the same as your TARGET_NAME. The full value in the Class field should be something like MyApp.entityName.

  2. Use only entityName in the Class field and prefix your swift class with @objc(entityName)

The representedClassName field in the data model appears to be evaluated at runtime so it needs a literal value.