Using subproject's xcdatamodelId in main project - ios

I have in XCode a main project that relies on a subproject to work. The subproject is basically a library that also define a database model, and the way to interact with it.
To use the subproject in the main project, I just dropped the subproject xcodeproj file in the main project, did some setup (like specifying header search path), and that's it.
I'm using core data, but if I try to use the .xcdatamodelId files directly, by creating managed object context, it fails : looks like the main project can't find the subproject ressources (except .h and .m files).
The only solution I found so far is to copy the .xcdatamodelId file from the subproject in the main project, and it does the trick. But it's not satisfactory, as I have to maintain two files if my model evolves.

I was struggling with this issue for a while and I want to answer it here as I believe it is important.
In order to add model files from a sub-project into the parent project you simply locate the model files you want to add in Finder. You can then drag them into your project into any folder you see fit. When Xcode displays the "Choose options for adding these files" prompt, deselect the "Copy items into destination group's folder" item and you can select either "Create Groups..." or "Create folder references...". After this process you should see the model files under the compile sources phase in Build Settings.
It is important to note that dragging the model files (in Xcode) from the sub-project into either "Compile Sources" or the parent project will not work to my knowledge so rather do it in Finder.

Related

Export files from Xcode project

I currently have an Xcode workspace with an app project and the pod project (as commonly happens). I would like to "export" some of the files containing classes and structs that would be preparatory for a new project that should begin soon. These files are totally separate from each other and should not be exported as a library or framework because that should require to keep everything updated. Instead, I would rather bring these files out of the app project, but keep them into the workspace. Is it possibile? If so, how can I achieve this goal?
When you want to keep your common code and be able to maintain it as one code on both projects then I suggest you still use a framework. It is hard to tell from your question what you mean by "because that should require to keep everything updated" but using a framework within your workspace works awesomely in Xcode and any change you do within framework is instantly reflected in your code. So no extra deployment work is needed at all. To do so you simply do the following:
Create a new project selecting Framework
When creating the project select your workspace from dropdown menu (it is at step where you select project folder location)
In Xcode file navigator in newly created framework project open "Products" directory which should contain (in red) FrameworkName.framework
Open your main project target settings and open General, scroll down to Frameworks, Libraries... and drag that .framework file in this list
That is all. Now you can use your framework in your code. Simply import it in any Swift file you want to.
If at this point you are worried that you need to add import MyFramework into each and every Swift file where you use it then there is a shortcut. You can simply add a new Swift file and use type aliases to avoid this problem:
import MyFramework
typealias Class1 = MyFramework.Class1
typealias Class2 = MyFramework.Class2
typealias Class3 = MyFramework.Class3
Not the cleanest solution but it does the job.
On the other hand if you wish to extract files and use them in a new project but not share the code. So then each of the projects may eventually end up with different implementations of these files then you need to duplicate the files. You can do that easiest with file system by copying source files themselves.
You can then put them into workspace under whatever you want. If you wish that currently none of the projects contain these files you may simply select files in Xcode and uncheck Target Membership for all projects on file settings.

Work with Frameworks in Swift

I'm following this steps and works perfect, but i need something additional.
Each time when i do any change, is necessary remove the .framework file from parent project and go to framework project and generate a build again and drag and drop into the parent project AGAIN.
It's possible make a reference in where only need run the framework project to see the changes in the parent project without need follow all this steps?
Build your framework in your framework project , right click on the .framework file in build folder , show in finder , drag it to your parent project , in the window that opens uncheck the "copy items ... " option.
This will create a reference to your framework build file and not a copy.

Use an entire app as a dependency?

Without going into the why, I need to use an entire app for various classes and libraries it has and includes.
Is there a way I can use the entire project as a dependency? When I choose "static library target" I lose all the hundreds of classes in the "compile" step. Effectively I would like to be able to package the project such that I can use it elsewhere.
Is there an easy way to do this aside from building my new app out of the old app and changing the app name/launch targets?
You have to find a way to transport every element into the new XCode project. iOS app consists of:
main.m file - You probably won't need to copy that, as it's usually just one line.
Source files and headers for: appDelegate, ViewControllers etc. - I don't understand why you would "lose all the hundreds of classes" during compile time. You're the one that chooses what is to be included in the static library. Add all the header files you need to "Copy headers" in the static library target "Build Phases". Add all the source files into "Compile sources". There are many tutorials and StackOverflow posts on how to do that. (example: How can I create static library and can add just .a file on any project in ios)
Storyboards, xib files, *.plist files, images and other resources - you need to put these in a *.bundle file . Just create a new target (which is a bundle) and include all the needed resources in it. Then you'll have to find a way to use them in the new XCode project. For example setting the default *-info.plist or *.pch file: How to tell Xcode where my info.plist and .pch files are or setting the main storyboard.
So you end up with two files: one framework/static library and one bundle file. It shouldn't be that hard to configure new XCode project to use resources from the bundle and classes from the static library.
I'm not sure if I understood your question.
You can add your old app as a project to your workspace and add it as a "target dependency" to use it.
The easiest and safest way to do this is to copy the whole workspace and change the initial view controller.

Header search paths for subproject in XCode

I added a subproject (a library) to my project.
In order to be able to import the header files of the subproject, I'm adding the absolute path to the subproject directory (recursive) to the "Header Search Paths" of my main project. Is there anyway to configure XCode to automatically/ dynamically recognise the path to my subproject directory?
I feel that what I'm doing right now is very manual and prone to human errors. Also, in case I moved my subproject, I will not be able to import its header files anymore.
Note that I don't want to put the subproject directory inside the same directory with the main project as it will be reused for many different projects.
The reason why I expected this functionality of XCode are:
XCode allows me to drag and drop my library project into the main one, which is an intuitive and dynamic way of including library. It should not, therefore, require me to manually put in the path to the main project's build settings.
When we drag and drop our files and directories into an XCode project, it seems to handle the XCode's references and the filesystem paths smoothly. I don't understand how the logic differs for header search paths for subprojects?
You can use a relative directory, as opposed to an absolute directory, by using $(PROJECT_DIR), which is one of Xcode's many variables.
Assuming the sub-project is in a sibling directory to the app project (a system I use myself), then set Header Search Path to (something like):
$(PROJECT_DIR)/../libproject/include
This will allow the project to exist anywhere in the filesystem, provided the project directories maintain the same relative path to each other.

XCode - Workspaces

i'm developing a framework and i would like to know if there is a method for share the code across multiple project. Example : i have my workspace with my framework project and a test application project for the fw; then, i have another project of an app and i need to use the framework, but i want to link the framework so that when i modify the original code in workspace even the code in the project is updated without import every time the .framework file.
Is it possible?
If not, how can i do to share code across projects?
Thanks
Yes, this can easily be done by importing files from the other project. You could create a group called external, right click and choose "add files". Make sure the option "copy items into destination group`s folder" is unchecked.
If you now change the files in the other project, the changes will be reflected in the dependent one.

Resources