8

I've opened up some old iOS code and when I try to build it I get an "unused parameter" error for code like this:

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    NSLog(@"Search Bar isn't used in this function");
}

This is the first time I've ever seen an Objective-C compiler spit out errors (not warnings) for this. Since a lot of iOS calls don't necessarily use the passing arguments (examples being a lot of callbacks), I need help in getting rid of this.

jscs
  • 63,694
  • 13
  • 151
  • 195
sparkFinder
  • 3,336
  • 10
  • 42
  • 57

1 Answers1

13

Solution # 1)

In your Xcode project's "Build Settings", there's a parameter for "Unused Parameters".

Reset that from YES to NO. Unused Parameters Warnings

Solution # 2 (available with Xcode 4):

In Xcode 4.3.2 or higher use __unused.

(THANKS to Tim Bodeit's comment below)

Solution # 3)

Put #pragma unused (searchBar) in your code, preferably right underneath the line in your implementation where the function is declared.

I.E.

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    #pragma unused (searchBar)
    NSLog(@"Search Bar isn't used in this function");
}
Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • Hi Michael, the XCode setting for the unused parameter warning already reads 'NO'. I'm really hoping not to have to inject code everywhere. Is there some other compiler (or other) setting I can look into? – sparkFinder Apr 04 '12 at 17:28
  • does the Setting read "NO" under the Resolved column for your ***target*** (not just the project)? Also, do you have "treat warnings as errors" set to yes in your project or target settings? – Michael Dautermann Apr 04 '12 at 17:36
  • It's listed as 'NO' in every column for both the project and the target. Treat warnings as errors is set to 'NO' in both as well. – sparkFinder Apr 04 '12 at 17:46
  • hmmmmmmmmmmmmm... you may want to edit your question to include the Build Log output of some of the errors you're seeing. I'm running out of ideas here. – Michael Dautermann Apr 04 '12 at 17:59
  • So I've gotten rid of the error by removing -Werror and -Wextra. The first tag makes warnings turn into errors. I don't like to have this off right now but I'm stuck. Going to accept your answer. – sparkFinder Apr 04 '12 at 18:09
  • I had to remove -Wall on older third party projects with the latest XCode 5.1 update or I'd get "unused warnings" into compiler errors even if I disable that particular warning in the project itself or as an independent flag. – Robin R Mar 16 '14 at 16:03
  • **CORRECTION** ^^ Above: The actual reason was "treat errors as warnings" compiler flag was enabled and Xcode has new warnings that it did not before so thus new errors showed up. Removing -Wall just made these new warnings disappear and thus not causes errors. – Robin R Mar 16 '14 at 16:41
  • The solution #2 is great. – Beto Mar 24 '14 at 22:02
  • In Xcode 4.3.2 or higher use `__unused` http://stackoverflow.com/a/10578498/1984384 – Tim Bodeit May 06 '15 at 13:22