Robert
Robert

Reputation: 38213

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?

Upvotes: 18

Views: 10310

Answers (5)

Fernando Martínez
Fernando Martínez

Reputation: 1077

Xcode assembles some env variables and one of them has the dir you want to look for:

LINK_FILE_LIST_normal_x86_64, for x86_64 target, it point to the LinkFileList, but from basedir you can get the actual dir with FileLists you may want.

Upvotes: 0

Eli Manjarrez
Eli Manjarrez

Reputation: 191

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.

Upvotes: 19

Justin
Justin

Reputation: 31

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.

Upvotes: 3

arsenius
arsenius

Reputation: 13256

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.

Upvotes: 1

bames53
bames53

Reputation: 88175

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.

Upvotes: 7

Related Questions