How to find and remove unused class files from a project
Asked Answered
S

4

13

My XCode project has grown somewhat, and I know that there are class files in there which are no longer being used. Is there an easy way to find all of these and remove them?

Shad answered 8/10, 2010 at 6:40 Comment(0)
M
4

If the class files just sit in your project without being part of a target, just click on the project itself in the tree view, so you see all files in the table. Make sure you see the "Target" column in the table view, iterate through your targets and find the files that don't have a check anywhere -> they are no longer compiled.

But if you still compile the classes and they are no longer used, that case is a bit more difficult. Check out this project

http://www.karppinen.fi/analysistool/#dependency-graphs

You could create a dependency graph and try to find orphaned classes that way.

Edit: Link went dead, but there still seem to be projects of Objective-C dependency graphs around, for example https://github.com/nst/objc_dep

Madore answered 8/10, 2010 at 9:40 Comment(2)
The target column may be misleading, in that it only considers the active target. If the class is not used in the active target but is used in another target, the target column will have no checkmark even though it is being used.Chiseler
That's why I suggested to try to find files that don't have a check in any of the targets. Of course, this won't be much fun if you have a lot of targets..Madore
D
2

if they are C or C++ symbols, then you can just let the linker do the work for you.

if you're looking to remove objc symbols, then try to refactor the class name (e.g. to rename the class), and preview the dependencies that it turns up. if you reference classes/selectors/etc. by strings then... it may not be so effective. unfortunately, you often have to also test manually, to verify that removing a class does not break anything. remember that resources (like xibs) may reference/load objc classes as well.

Dolt answered 8/10, 2010 at 9:56 Comment(0)
H
2

This is a tricky question due to how dynamic objective-c is as you can never guarantee that a class is not going to be used.

Consider if you generate a class name and a selector at run time and then look up that class, instantiate that class and then call a method on that newly created object using that newly created selector. No where in your code do you explicitly name and instantiate that object but you are able to use it anyways. You could get that class name and selector name from anywhere outside of your code, even from some data from a server some where. How would you ever know which class is not going to be used? Because of this there are no tools that are able to perform what you are requesting.

Hardset answered 8/10, 2010 at 15:44 Comment(0)
E
1

Searching the project with the class name might be an option, thought it may not be the best solution. Specially it might be time consuming when you have many classes.

Enterogastrone answered 8/10, 2010 at 6:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.