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

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:
warning: The Swift file "/Users/reiner/Library/Developer/Xcode/DerivedData/
MY_APP/Build/Intermediates.noindex/MY_APP.build/Debug-iphonesimulator/
MY_APP.build/DerivedSources/CoreDataGenerated/MY_APP/XXX+CoreDataClass.swift"
cannot be processed by a Copy Bundle Resources build phase (in target ‚MY_APP‘)
warning: The Swift file "/Users/reiner/Library/Developer/Xcode/DerivedData/
MY_APP/Build/Intermediates.noindex/MY_APP.build/Debug-iphonesimulator/
MY_APP.build/DerivedSources/CoreDataGenerated/MY_APP/XXX+CoreDataProperties.swift"
cannot be processed by a Copy Bundle Resources build phase (in target 'MY_APP')
warning: The Swift file "/Users/reiner/Library/Developer/Xcode/DerivedData/
MY_APP/Build/Intermediates.noindex/MY_APP.build/Debug-iphonesimulator/
MY_APP.build/DerivedSources/CoreDataGenerated/MY_APP/MY_APP+CoreDataModel.swift"
cannot be processed by a Copy Bundle Resources build phase (in target 'MY_APP')
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.

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.

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.

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.

Related

How can I debug in a framework in Xcode?

I have two projects, one is the networkLib for login and some other network function, the other is the usingLibDemo. So I have all the source code of both projects.
The networkLib project outputs a framework, called myNetKit.framework, which is used by usingLibDemo.
Now I successfully use myNetKit.framework to login, but sometimes it crashes, maybe in main() without stack information, but sometimes Xcode gives me the stack info like below:
So I know where it crashes:
But the Utils.m is not exposed, how Xcode gets the stack info and the crash line, and eventually open the source file for me? Because that I have the source code in my disk?
If so, how can I debug the myNetKit.framework step by step, when it is not crashed?
Thanks a lot for any tips.
If someone would have the same question in future (now I am using Xcode 8):
You can:
build your framework project (in this example "networkLib")
copy project output ("myNetKit.framework") to destination project ("usingLibDemo")
run destination project ("usingLibDemo") on your device
stop process
launch app from your device manually ("usingLibDemo")
open framework project and Attach to Process of your app (launched in step 5). In Xcode 8: Debug->Attach to Process-> Select name of your app.
Don't forget set some breakpoints in your framework project.
When the library is built with all symbols, it contains full paths to the each source file embedded in itself. You can actually see this if you open the .a with a hex viewer. With this in place, the XCode will know how to get to the source file.
Setting breakpoints is somewhat more challenging. You basically have to make XCode slowly discover source files from your library by stepping into methods in those file. Once XCode has opened the file, you can set the breakpoint anywhere in it.
It is a bit painful but it works and you do not have to make the library project a subproject if you do not want to.
The following works considering the scenario that you have the framework project separately and added a.framework to some project B.
Go to your project B, add a breakpoint anywhere.
Go to the breakpoint view(where all breakpoints can be seen as a list), right click your breakpoint and click move breakpoint to user.
Go to a.framework and repeat step 1 and 2 but for your framework project.
Now switch back to project B and run the project on simulator/device.
You'll now see program stopping at the breakpoints set in the framework as well.
You can also add the entire library (networkLib) project into your project and link the library dynamically by adding dependency in project settings. So you can have all the source code within your project. So you can debug it in run time.
Use XCode Workspace when you deal with multiple framework projects. When you use a workspace, breakpoints will work and you can find your crash without loads of back and forth debugging. It will be much easier to manage your frameworks in the long run.
You should debug the project networkLib which outputs the framework separately. The framework do not have app like structure so a framework file within other project can't be debugged.

Xcode 4 - renamed/deleted file, yet get 'file not found' error

I just used Xcode's refactoring tool to rename a core data class from Player to Person. After fixing about 100 errors resulting from using dot notation that Xcode missed in the rename, I got to the last one:
Player.m - Lexical or Preprocessor issue - 'Person.h' not found
Player.m no longer exists in the project, and shows up as deleted (empty icon) in the error. Obviously, since I got rid of it, I don't want it to exist anymore!
To fix this, I have tried re-adding an old copy of Player.h and Player.m, and also cleaning. I have checked the Build Phases->Compile Sources section, and Person.m is there, Player.m is not. Person.h does in fact exist.
Not sure if it matters, but I am using mogenerator to generate my core data classes. Also, this is my third xcdatamodel version. Versions 1 and 2 used Player.h.
Here is a screenshot of the error:
Try clean build, if that doesn't work, restart Xcode, if that doesn't, delete the DerivedData directory, maybe even reboot.
Xcode sometimes gets confused.
You can try to do a super clean (not sure what the real term is) by hitting Cmd+Alt+Shift+K. This performs a clean and removes everything from the build folder.
Well I had a very similar problem and solve it removing the missing compile source of my project settings. to do that follow these steps
Click at the project page. This page contains the general settings
of your app
Select Build phases.
In Build Phases section, select compile sources.
If there is missing some class or file or something that needs to be compiled.
Select water mark file and click the minus sign at the bottom of this section.
Just click and the project may run. I hope this help others.

Xcode 4 embedded resource prevention

In XCode4, is there a way to have a 'resource' (i.e. a CSV used to load a sqlite database) that is there for project purposes, but not have it compiled into my project? So my final app doesn't include it? I can't seem to see to to have this facility.
Thanks
Assuming you do not want the file get into your .app file, One solution i can think of is, do not include 'Target' when adding the file to the project. If you want to exclude your already added file then
Choose the target
Go to Build phases
Expand copy bundle resources
Select your resource file and remove it.

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

I'm working on an Xcode 4 project template and I'm struggling with controlling the files that get added to the Copy Bundle Resources build phase--header files get added that I do not want to be copied into the project bundle and a file that I do want to be copied does not appear in this list (a custom .framework file that contains both the static library and image resources).
It seems like Xcode automatically builds contents of this build phase from the Nodes section in the project template; if it's a .framework, it automatically gets added to the linked libraries, if it's a .m, it automatically gets added to compiled sources, and everything else gets added to the Copy Bundle Resources.
I'd be grateful for any direction on this one!
Update:
To clarify, I'm attempting to create an Xcode project template that, when used, creates a new Xcode project that includes the specified files from the template in the Copy Bundle Resources build phase.
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.
Yes, Xcode tries to manage this for you. If after adding a file, it did not did what you want, then change it. It's guesses are "usually" correct "most" of the time. :)

iPhone "_OBJC_CLASS_" compilation error

I am trying to compile my project in Xcode. But i am getting the following error.
"_OBJC_CLASS_$_InfrastructureBenchmarkingViewController", referenced from:
objc-class-ref-to-InfrastructureBenchmarkingViewController in RootViewController.o
4 more of similar type.
Can't understand the meaning of this error?
Small Clarification: Actually the _InfrastructureBenchmarkingViewController was a module in another project. The files pertaining to that I have copied into the new application.
Sometimes it happens when the project definition file (.xcodeproj/project.pbxproj) is somehow messed-up, eg. because of XCode crash or project is taken from someone else, etc.
So try removing InfrastructureBenchmarkingViewController (Delete the file, then choose "Delete reference", so it is not deleted physically from disc) and then again add this file to project (Add > Existing file from the project context menu).
It worked for me several times.
you need to link in whatever framework _InfrastructureBenchmarkingViewController is a part of. Try going to the project window, right click on frameworks and choose add existing framework ....
you should be able to find the framework including _InfrastructureBenchmarkingViewController there.
Seems you have some object files that are not valid anymore. Use the clean option from menu and build it again. That should solve it.