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.
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 ?
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.
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.
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:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With