Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find unused ivars in Xcode

Sometimes I declare an ivar but after a while I am no longer using it. I would like to remove this sort of cruft from my code, but I cannot find a warning that will show me my unused ivars.

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

I see that the static analyzer has CLANG_ANALYZER_OBJC_UNUSED_IVARS, but it does not seem to do anything.

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

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

like image 837
Skotch Avatar asked Jan 13 '14 22:01

Skotch


People also ask

Can the Xcode analyzer detect unused symbols?

So, the XCode analyzer could at least do a text search, and provide a warning, not with certainty, but pointing to the possibility that a symbol is not being referenced. The developer could then investigate the warning and determine if the warning is false or not. Why would this be a good way to point out potentially unused classes/functions ?

How to check what resources are not being used in Xcode?

It's an useful utility tool to check what resources are not being used in your Xcode projects. Very easy to use: Click Browse.. to select a project folder. Click Search to start searching. Wait a few seconds, the results will be shown in the tableview.

What is the difference between unused and unused in Xcode?

It is heavily influenced by jeffhodnett‘s Unused, but Unused is very slow, and the results are not entirely correct. So I made some performance optimization, the search speed is more faster than Unused. It's an useful utility tool to check what resources are not being used in your Xcode projects.

How do I find unused declarations in Swift code?

Periphery is an open-source tool written in Swift that aims to find unused declarations in your code. You can install one easily via homebrew: After installation is complete, run the following command:


2 Answers

Based on the relevant Clang source code and a couple of quick tests, it seems that the analyzer does not look at ivars that are not both declared in the @interface and marked @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 filing a bug/submitting a patch.

like image 101
jscs Avatar answered Oct 20 '22 18:10

jscs


It appears that the static analyzer option only works if you declare the ivar in the header file.

This generates the analyzer warning correctly:

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

Neither of these generates any sort of analyzer warning:

//  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 the modern syntax to keep ivars in the .m file if you want to check for unused ivars.

like image 38
Skotch Avatar answered Oct 20 '22 19:10

Skotch