Xcode generated CoreData files cannot be processed by Copy Bundle Resources build phase

匿名 (未验证) 提交于 2019-12-03 01:39:01

问题:

I added CoreData to my app MY_APP:

  • I defined the data model by creating a xcdatamodeld file containing a single entity XXX with a few attributes.
  • Using Xcode/Editor/Create NSManagedSubclass, Xcode created 2 files, XXX+CoreDataClass.swift and XXX+CoreDataProperties.swift.
  • I wrote a little code to test storage and fetch back from core data, and everything works fine.

The problem:
At the beginning of the build phase, I get 3 warnings:

These 3 files are not listed under MY_APP target/Build Phases/Copy Bundle Resources.

My questions:
Is anything wrong with my build setup, i.e. what is the reason for these warnings, and how can I avoid it?

Remark: This question relates to a different framework (core data), but is similar to this one, which does not have an answer yet.

EDIT:
My project has 2 targets, for iOS and for watchOS. Until now, core data was used only on iOS.
I tried now to enable it also for watchOS, but I got an error, because the .xcdatamodeld was not yet in Build Phases / Copy Bundle Resources.
As soon as I added it there, core data was executed correctly on the watch.

BUT: I got the same 3 warnings mentioned above, this time additionally for the watch extension target (altogether 6 warnings).
Maybe this a useful hint.

回答1:

EDIT:

I contacted Apple, and they provided a solution:


Building the project succeeds, and one can use a property Entity.attribute as usual in the code.

However:

This means that these files are added twice, thus the warnings.

So the solution is not to use this editor command, and I don’t know what it is for…

EDIT 2:

My fault; I was looking at the wrong place:

  • Open the xcdatamodeld in the project navigator.
  • In the pane right of it, select an entity.
  • At the top right, open the inspector pane.
  • At the top right of it, select the data model inspector.

If this option is selected, no code is automatically generated from xcdatamodeld, i.e., one can manually (by using the editor command) create NSManagedObject subclasses that can be added to the target Compile Sources section, as required.

Previous answer:

There are apparently 2 ways to use CoreData, either 1) by using only the PROJECT.xcdatamodeld file, which is then added to Compile Sources Build Phase, or 2) by creating a NSManagedObject subclass using Xcode’s Editor/Create NSManagedObject Subclass command.

If 1) is used, everything works fine, but one has no property access to the entity used.

If 2) is used, Xcode creates the 2 files ENTITY+CoreDataClass.swift and ENTITY+CoreDataProperties.swift. These 2 files are added to the Compile Sources Build Phase, but PROJECT.xcdatamodeld must not
However, when run, the instruction

let entity = NSEntityDescription.entity(forEntityName: "MyEntity", in: managedContext)! 

fails, because it does not find the data model.
A workaround is to add PROJECT.xcdatamodeld to target / Build Phases / Copy Bundle Resources. Then the code executes fine, but one will get the warnings that I described in my question.



回答2:

For me what caused the issue was having the .xcdatamodeld file in the Copy Bundle Resources step within Build Phases for the target specified in the warning: in your case, MY_APP. I removed that file from the Copy Bundle Resources step and all the warnings went away.



回答3:

An approach to dealing with an Xcode which seems to be getting more and more buggy with each release in recent years:

  • Quit and relaunch Xcode.
  • If that does not work, do a Show Package Contents on the .xcodeproj package and open the .pbxproj file in a text editor. Search the file for occurrences of XXX+CoreDataClass. The search the file for occurrences of some other .swift file which does not create this warning. Compare the two search results. It may be necessary to manually edit the .pbxproj file.


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