Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find all files in Xcode that are NOT a member of a target

One of the most common issues I have with multiple targets is forgetting to add a file to all targets. Its easy to import a file and not tick all the boxes...

Many Targets

In build phases there is a 'compile sources' view which is a list of files compiled for each target. However there seams to be no easy way to compare targets to see what files are in one but not the other.

Is there a way to find the files that are NOT a member of a particular target? A script perhaps?

like image 308
Robert Avatar asked Jul 04 '12 09:07

Robert


People also ask

How do I check my target membership Xcode?

You can access Target Membership by selecting file and opening the right menu in Xcode (the menu is called Inspectors ). Then, in File Inspector at the bottom of the menu, find Target Membership . It's very important to check all files in your project because without this you won't be able to build a new app target.

Where can I find Xcode files?

In xcode on left pane in the project navigator at the bottom there is a search field. In the documentation it is called filter bar. According to Inder Kumar Rathore the shortcut is ⌘ + ⇧ + O .

How do I search all projects in Xcode?

Find Text or Patterns in Your Source Code To find text in a file, open the file in the Xcode source editor and choose Find > Find from the menu bar. Xcode displays the Find bar and its search controls at the top of the file. Enter a search term. Xcode searches the file, highlights matches, and notes how many it finds.

What is Xcode target membership?

Making an implementation file a member of a target tells Xcode to compile the file when you build the target. In your example Xcode compiles the file myAppFile. m when you build the myAppTests target but not when you build the myApp target.


5 Answers

Assuming your MyProject.xcodeproj is in your working directory, and all your sources are in or below your working directory, you could use a hack* like this to look for .m files that are not part of any target:

find . -name "*.m" -exec basename '{}' \; | xargs -I '{}' sh -c 'grep "{} in Sources" MyProject.xcodeproj/project.pbxproj > /dev/null; echo "$?\c"; echo " {}"' | grep "^1"

Once you remove the files from the project, you can remove them -- and other objc source files that are no longer referenced from your project -- from your git repo using this similar script:

find . -name "*.[hm]" -exec basename '{}' \; | xargs -I '{}' sh -c 'grep {} MyProject.xcodeproj/project.pbxproj > /dev/null; echo "$?\c"; echo " {}"' | grep "^1" | cut -c3- | xargs -I '%' find . -name '%' -exec git rm '{}' \;

* hack in the sense that it depends on implementation details of the Xcode file format.

like image 127
Eli Manjarrez Avatar answered Oct 19 '22 23:10

Eli Manjarrez


I don't know of a way to script it, but for a file you can view a list of all targets with the associated targets checked, and you can check or uncheck targets from that list.

Open the Utilities pane and select the File Inspector. Then select a file and the related targets will be in the Target Membership area of the File Inspector.

You can even select multiple files and the targets that are associated to some but not all the selected files will be marked with '-'. If you check a target then all the selected files will be added to that target.

like image 36
bames53 Avatar answered Oct 20 '22 00:10

bames53


I built a tool which lets you extract what files are in targets

https://github.com/ConfusedVorlon/ProjectFileTool

You could compare this with a file search for .m files within your project directory.

like image 33
Confused Vorlon Avatar answered Oct 19 '22 22:10

Confused Vorlon


In Xcode 8, listing the files from the Target -> Build Phases -> Compile Sources gave a list of files for each target but no easy way to find the missing ones. Selecting all files and copying them allows copies of the physical files to be pasted into a temporary directory. After doing this with both targets, into separate folders, duplicates can then be deleted until only files missing from the other target are left in each folder.

I've just had a "this class is not key value coding-compliant for the key" crash caused by a file not being included in a target so I had to find the missing files. Not an efficient or elegant solution but it got the job done.

like image 41
Justin Avatar answered Oct 19 '22 22:10

Justin


If you don't know which files you added to a target, you can go to Build Phases->Compile Sources (or whatever section you have differences in). Then select everything and hit copy. Paste this into a diff program, select the other target and repeat.

like image 41
arsenius Avatar answered Oct 20 '22 00:10

arsenius