How to find unused ivars in Xcode

Sometimes I declare ivar, but after a while I no longer use it. I would like to remove this type of crack from my code, but I cannot find a warning that will show me my unused ivars.

Is there a tool or built-in Xcode function that will allow me to find all my unused ivars?

I see that the static analyzer has CLANG_ANALYZER_OBJC_UNUSED_IVARS, but it does nothing.

@implementation AppDelegate { @private BOOL _foo; // Never read or written to } 

Starting the analyzer in Xcode 5 with CLANG_ANALYZER_OBJC_UNUSED_IVARS (unused ivars) set to YES never causes a warning.

+6
source share
3 answers

Based on the appropriate Clang source code and a few quick tests, it looks like the analyzer does not look at Ivars that are not both declared in @interface and marked with @private .

 @interface Igloo : NSObject { NSString * address; // No warning @private NSInteger radius; // Warning } @end @implementation Igloo { NSInteger numWindows; // No warning @private // Has no real effect, of course; just testing NSString * doormatText; // No warning } @end 

I suggest submitting an error / submitting a patch.

+4
source

In Xcode, from the product menu, click on analysis ... It will show you unused variables. It will also tell you about the dead code.

+1
source

It looks like the static analyzer parameter only works if you declare ivar in the header file.

This generates an analyzer warning correctly:

 // AppDelegate.h @interface AppDelegate : NSObject <UIApplicationDelegate> { BOOL _foo; // never read or written } @end 

None of them generate any analyzer warnings:

 // AppDelegate.m @interface AppDelegate () { @private BOOL _goo; // never read or written } @end @implementation AppDelegate { @private BOOL _hoo; // never read or written } @end 

So it looks like you cannot use modern syntax to keep ivars in a .m file if you want to check for unused ivars.

+1
source

All Articles