Core Data Codegen fail in Xcode 8

女生的网名这么多〃 提交于 2019-12-02 22:43:27

Regarding Problem 1, setting the Codegen popup in the Inspector for an entity in an .xcdatamodel is supposed to work like this:

Category/Extension tells Xcode to generate one file, ClassName+CoreDataGeneratedProperties.

Class Definition tells Xcode to generate two files, the above-named file, plus ClassName+CoreDataClass.

However, there appears to be a bug, in Xcode 8.2. If, after changing one of these popups, you simply Build (⌘B) or Run (⌘R), your changes will not take effect. For example, if you changed from Class Definition to Category/Extension and even manually deleted the second file, it will reappear. You may even find that the popup reverts back to the original setting.

In order for changes in these popups to become effective, you must

  1. Save (⌘S) the .xcdatamodel file.
  2. Close all project windows.
  3. Re-open the project.

During the next build (which may occur automaticallly when the project opens if you have an @IBInspectable in a storyboard), the affected files in Derived Data will be generated or deleted to conform to your new setting.

UPDATE 2016-12-22: Thank you for the comment, Ashley. I've now written up and submitted this to Apple Bug Reporter: 29789727. Update 2017-02-08: Apple has closed 29789727 as it supposedly duplicates 21205277.

Problem 4 can be fixed (at least in the latest beta, v. 6) by selecting all the entities you want auto-generated, and then in the inspector clearing the Class -> Module field so that it defaults to "Global namespace".

Regarding problem 2: Core Data's "optional" flag has nothing to do with the Swift concept of an optional. They are unrelated and do not mean the same thing. Marking a Core Data attribute as non-optional does not imply that it's non-optional as Swift defines the term. The difference is:

  • Swift non-optionals must have non-nil values at all times.
  • Core Data non-optionals must have non-nil values when you save changes, but Core Data neither knows nor cares whether they're nil at other times.

Problem #4 sounds like Xcode getting its state confused, and may be fixed by voodoo like manually clearing the derived data folder (again, I know). I can't currently reproduce it, but that doesn't mean it's not a bug in the current beta.

It's actually pretty easy to fix it.

Go to configuration:

Then delete the points. These are only there in older projects. The import statements are fixed after this and you are good to go.

You also should put your models to the Global namespace. Mine were in a extra namespace, but i have no idea why.

Apples approch is that you can use this in a custom framework.

This feature is pretty nice, if you get it working ;)

Looks like Xcode 8.2.1 won't turn off codegen. But you can manually delete codeGenerationType="category" from .xcdatamodel contents file.

Close Xcode, delete codeGenerationType, delete DerivedData folder and rebuild.

Will wait for fix in future releases.

I got completely stuck with Problem 4 and none of the above worked for me. I use code generation. To resolve it I did the following steps:

  1. I opened the .xcdatamodel / content file in a text editor. (In Xcode right-click your .xcdatamodel and choose "Open in external editor"
  2. In the XML file I made sure that each entity has codeGenerationType="class"
  3. Close and reopened Xcode Cleared my derrived data folder
  4. It still refused to do the Code Generation, so I then generated each Entity manually (Editor, Create NSManageObject Subclasses, select all entities)
  5. Build the project. Now it also did the Code Generation and compiler errors appeared saying that the Entity filenames were used twice.
  6. Deleted the Manually created NSManagedObject subclasses created in step 5.
  7. After that the project built again successfully and the problem disappeared.

PS: If you choose to Manually create your NSManageObject subclasses rather then using Code Generation, then make sure you remove codeGenerationType="class" from your entities in the XML file in step 2.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!