Generating Swift models from Core Data entities

前端 未结 7 1547
我在风中等你
我在风中等你 2020-12-08 09:36

Update for Xcode 8:

In Xcode 8, one needs to go to the Core Data Model Editor and Show the File Inspector. Near the bottom is an option for code gen

7条回答
  •  孤城傲影
    2020-12-08 09:54

    You can get Swift model back using NSEntityDescription.insertNewObjectForEntityForName but you must edit your core data model file and not use Person as a Class Entity but .Person else it returns NSManagedObject...

    Using println() you won't see Person instance but something like <_TtC5ProjectName4Person: 0xc9ad5f0> but calling methods on this will prove it's a Person instance for real. I guess it's just the way for Swift to generate unique class names, not conflict and CoreData methods show this internal mechanism.

    The Apple documentation says:

    Swift classes are namespaced—they’re scoped to the module (typically, the project) they are compiled in. To use a Swift subclass of the NSManagedObject class with your Core Data model, prefix the class name in the Class field in the model entity inspector with the name of your module.

提交回复
热议问题