34

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

bandejapaisa
  • 26,576
  • 13
  • 94
  • 112

7 Answers7

61

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.

screenshot

saurabh
  • 6,687
  • 7
  • 42
  • 63
bandejapaisa
  • 26,576
  • 13
  • 94
  • 112
7

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.

user1050660
  • 461
  • 3
  • 3
2

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.

zaph
  • 111,848
  • 21
  • 189
  • 228
  • I said easy, and there is a lot of files in a lot of directories..... selecting all the .m files in the project explorer is tiring. – bandejapaisa Oct 14 '11 at 10:31
1

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.

Erdem Gezer
  • 3,142
  • 2
  • 22
  • 16
1

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

justin
  • 104,054
  • 14
  • 179
  • 226
0

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.

Kreeble Song
  • 121
  • 1
  • 8
  • Can you provide any documentation and / or tutorial? I quite did not get your answer but I'm curious to know more about it. – Otávio Nov 03 '16 at 12:48
  • 1
    Read online about application vs. logic tests (logic tests are smaller and faster), but when you add a new test target in Xcode, it defaults to using your main target as your host app. What this means is your tests have access to everything in your main target bundle (code, resources), thus you can import any of your classes (even view controllers) - without having to explicitly include them in your test target. Thus, no need to manually click each .m file and include them in your app test target. – Kreeble Song Nov 03 '16 at 18:30
0

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:

Target Membership

Dima G
  • 1,945
  • 18
  • 22
  • Where can I find the Target membership in the Xcode 13.2.1 – sejn Jan 20 '22 at 04:34
  • @sejn , it is part of the xcode's inspectors panel. If it is not open by default, then look the top right corner of xcode. Note, it appears only when you have one or more files selected in the project navigator. – Dima G Apr 03 '22 at 09:59