Xcode easily add multiple files to a target - xcode

I've created a new target for unit tests in Xcode 4.2, and obviously i need to bring in certain application files into that target. But there is a lot, and it seems cumbersome to click each .m file then click TargetMembership and tick the new target box.
Is there an easier way to do this in Xcode that i'm missing?
Thanks

I though't i'd return to this with an improved answer. It's still difficult to do this via Xcode main interface, as if you filter for .m and try and do CMD-A to select all, it selects the folders too, so you can't add these to the target.
However, if you go to Project Settings > Build Phases > Compile Sources. You can click the + button, filter .m files, and you can CMD-A to select all here.
This works.

If the files are all in a single directory, delete the directory from the project (remove reference only), then re-add it. Xcode will ask you which targets you want the files to be added to when you re-add it.

Select all the .m files in the "Project Editor" (cmd-1), show the "Utilities View" (opt-cmd-0) and click the new target in "Target Membership". Be careful not to select files that do not belong to a project such as .h files.

create a static or dynamic library, to which both executables link.

If you duplicate and existing target instead of creating a new one from scratch, all the existing files will be a member of the new(duplicated) target as well.

In 2016 you can now use both an AppTest and LogicTest target so you don't have to fiddle around with target settings.
You don't need to add any files to the AppTest target since it will have access to all main target files, and put pure logic tests in LogicTest.

In Xcode 10
You can also select multiple files in Project navigator and then, in the "Inspectors" right side pane you can modify Target Membership:

Related

In Xcode how do I create a new workspace and add 1 project to it so that I can see the project and its files?

In Xcode how do I create a new workspace and add 1 project to it so that I can see the project and its files?
If I just drag in the project, all I can see is the project it cannot be expanded, I cannot see the files. Also, is there anyway to copy over existing schemes from the project so that they can be used in the workspace?
First choose file > new > Workspace...
Then, you must first, and this is important, you must first close the project that you want to drag in. Than drag in the .xcodeproj file from the finder to your new workspace. Then it will appear properly and expand to show all of its files and your schemes will be available.
Although the accepted answer is correct, there is an alternate way to accomplish the same thing, and it is arguably easier/more straightforward. You can just open up the project in Xcode and click File -> Save as Workspace...
Bam! You now have a workspace with 1 project in it, as requested.
Just FYI, 2020, Apple recently added a thing,
make your workspace, exit it and
make your new project. in fact when you enter the name to save the project,
they added this option:
just select the workspace you wish to add it to.
Besides all above solutions you can use this alternative. Let's say you have a workspace and a project that you want to add to that workspace.
Close your project.
Open your workspace.
Option + Click in the Project navigator. Make sure you are clicking on an empty area, not on the existing files or folders.
In the context menu, select "Add files to your workspace...".
Finally select your project xcodeproj file on you disk.
Have a nice project 😉,
Add project into workspace
As alternative
- Close a project that you are going to add to a workspace
- Open Project Navigator -> Right click -> Add Files to "<workspace_name>"...

How to Importing files from one project to another

I want to copy some files "Classes" from another project to my current project, but when I run it I get dozen of errors, its seem there are some steps to copying file from one project to another project using Xcode ,
I can make a collection of files "Classes" accessible in my project?
One project is Objective c, the another one seems to be Objective C++
Individual Files
Organize your files
Place the files you will need at a location that is accessible by the Xcode project. Usually, you place these files is a directory below your Xcode project, but that is absolutely not necessary.
Drag & drop
Grab all the files you need and drop them onto your project organizer. You will be prompted with this dialog, to which the settings should look like this (ensure that your target is selected):
Projects
If you want to include a Cocoa Touch Static Library, in the form of an Xcode project, drag & drop will suffice.
No there are not exactly steps we need to follow. Just make sure all dependencies are fulfilled while importing classes from other project. Don't forget to click at checkbox Copy files if needed so that Xcode can make separate copy for your project.
In order to import files in your project click following sequence:
command + option + A
or you can also add files by
Xcode -> File -> Add files to "Project Name"

Xcode: Project and Target, add resources to target?

I'm building my first app and I'm a bit confused about all the project and target settings. So far I understand that a project can have multiple targets, for example a lite and a full version. So first I'm building my full version and later on adjusting it for the lite one.
My question is now about the resources you add to the targets. So far I've never checked the checkbox "Add to target …" while adding images, fonts etc. Still it works fine. So why or why shouldn't I check this box? And if I need to check it, how can I do this for all the images etc. I've added to the project?
And I think in my resource folder are still some resources I don't need anymore and actually deleted in Xcode (correctly, not just deleting the link basically)..how can I get rid of them?
When you add a file/resource in Xcode (either using "New File" or "Add File to "), you will see a checkbox for all the targets in your project. You can check the ones that you want to include the new file/resource. For the existing file/resources in xcode, you have following two options:
For your target->Build Phases->Copy Bundle Resources, you can add resources to the target.
Click on the file/resource in project navigator, under "Utility View"->Target Membership, you can select/deselect the targets that need to include/remove this file/resource.
Regarding deleting resources, when you delete a resource or any file in XCode, it will give you options: "Remove Reference" or "Move to Trash". For both of these options, resource will be removed from all the targets and XCode project file. However, if you choose "Move to Trash" it will also be removed from the folder in your hard disk.

Xcode: Is there a location/flag to prevent a Class from compiling?

Is there a place (or flag) in Xcode for files that you don't want to compile? There are some classes that are/may become part of a project but currently won't compile. The main project doesn't link to them but Xcode still tries to compile them. Is there a way to prevent blocking the rest of project from compiling until these new Classes are "ready"?
Note that for every source file you can specify which target(s) it belongs to - look at the inspector window for a file (Get Info) and then hit the Targets tab. If you deselect a target for a given source file then it won't be compiled as part of the build process for that target.
[This amounts to much the same thing as what Eimantas has said in his answer - it's just a different way of looking at it.]
Look for unneeded files in "Compile sources" in Target -> {AppName} branch. Remove them from there and they won't be compiled on next build (make sure to Clean before you Build again)
In Xcode 8.3.3, in the utilities window, click on File Inspector tab at the top of the window. Uncheck the file in the Target Membership area of the File Inspector. Please see image below.
You can use preprocessor statements:
#ifndef HIDE_<insert name here>
CODE
#endif
And then use:
#define HIDE_<insert name here>
above the aforementioned code in the files you don't want to compile.

Xcode Different Resources For Different Targets

I am developing an iPhone app and there will be a Full as well as a Lite version of that app. In order get both bundles from the same source code and Xcode project I added another target to the Xcode project.
Now, I want to have the Lite target copy only a subset of the resource files to the bundle. But, Xcode won't simply let me delete individual files from the "Copy Files to Bundle" build step, since I imported all my resources as folder references. I need this in order to maintain a directory structure in the resources directory.
How do I solve this problem? Any suggestions or ideas are greatly appreciated!
In the left-hand panel of Xcode, right-click on the "Groups & Files" bar at the top. Check "Target Membership".
Now you'll see a tick against every file that should be included in your current build target. Untick the ones you don't want, switch to your other target, and repeat.
Instead of manipulating the "Copy Bundle Resources" step of your target, you can right-click resource files or groups (folders), choose "Get Info," and selectively leave stuff out of your Lite build under the Targets tabs of the info window for the file(s) you selected. Doing this on a group (folder) recurses the changes, so using resource groups (folders) is a nice easy way to make separate resources collections for separate builds.

Resources