Xcode Generated Coredata Files Cannot Be Processed by Copy Bundle Resources Build Phase

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

EDIT:

I contacted Apple, and they provided a solution:

When an Xcode project „xxx“ is created with the coreData option on, a core data model file „xxx.xcdatamodeld“ is created and added to the target Build Phases Compile Sources.

Say, one adds there an entity „Entity“ with an attribute „attribute“.

If one selects in the Xcode project navigator this model file and opens the file inspector, there is an entry „Code Generation“ which is set to Swift by default. This creates the 2 files Entity+CoreDataClass.swift and Entity+CoreDataProperties.swift automatically, but they are not shown in the project navigator.

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

However:
If one selects the xcdatamodeld file in the Xcode navigator, the Xcode Editor menu has an entry „Create NSManagedObject Subclass…“. If one selects this entry and there the xxx data model, the 2 files Entity+CoreDataClass.swift and Entity+CoreDataProperties.swift are created again and shown in the project navigator, and added by default to the target.

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.
  • There is an entry „Codegen“ where one can select Manual/None.

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. If one does anyway, one gets the build error „unexpected duplicate task“. But if one does not, the project builds without errors and warnings.

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.

How do I add files to the Copy Bundle Resources build phase with an Xcode4 Project Template

screenshot

This should help. After the Class-Resources key-value pair, you can add files somehow. I couldn't figure that out but I'm sure you can find it somewhere.

EDIT: Actually, i think an easier way is to in the Definition part, after the Path key, add a TargetIndices key (an array). With some experimenting you should be able to find out what value to put on one item of that array to put it in the Copy Bundle Resources build phase.

Variables to access the copy bundle resources xcode build phase files?

You should use one of the NSBundle methods. The most basic is:

NSString *path = [[NSBundle mainBundle] pathForResource:@"filename"
ofType:@"txt"];

Have a look at the NSBundle Class Reference for methods where you can specify subdirectories and how to get other bundles than the main bundle.

Warning: The Copy Bundle Resources build phase contains this target's Info.plist file

https://developer.apple.com/library/content/qa/qa1649/_index.html

Excerpt:

You are getting this warning because you probably added your Info.plist file to your Copy Bundle Resources build phase as shown in Figure

Sample Image

The INFOPLIST_FILE build setting specifies the name of the Info.plist associated with your target. When building a target, Xcode reads this build setting and copies the referenced Info.plist into your application bundle. Because Xcode automatically processes the Info.plist, you should not add it to your Copy Bundle Resources build phase or make it a target member.

To resolve this warning, select your Info.plist from the Copy Bundle Resource build phase as shown in Figure 1, then click the Remove (–) button to delete it from the phase.

Copy files in Xcode project during post-build phase?

I had it figured out. Instead of adding them to through the Resource Tag, I defined then under Build Phases->Copy Files. The key is to set the Destination option to Wrapper, which indicates that the files/folders will be copied to the myApp.app folder level.

iOS: Can't compile CoreData model because of Fetched Indexes in Xcode 9

This is fixed in Xcode 9 beta 2, according to the release notes:

The Xcode data model editor supports creating fetch indexes from
attributes, expressions, and relationships. (32407895)



Related Topics



Leave a reply



Submit