开发者

How to disable the "local declaration of 'foo' hides instance variable" warning?

开发者 https://www.devze.com 2023-03-12 15:53 出处:网络
I understand wha开发者_JAVA技巧t the warning says. This is exactly how scoping rules work. I appreciate that some people want a nanny. I don\'t. How can I disable this warning?You can\'t disable this

I understand wha开发者_JAVA技巧t the warning says. This is exactly how scoping rules work. I appreciate that some people want a nanny. I don't. How can I disable this warning?


You can't disable this warning in the current version of XCode. There is no build setting for it, nor does it have a warning ID which you could use to pass a flag to the compiler to tell it to quit whining.

For future reference, you can find that warning ID by going to the Log Navigator, clicking on the most recent build where the warning appeared, drilling down into the log to find the point where the 'compile' task shows up with an exclamation mark and click the 'more detail' button, which looks like a gray callout with 5 horizontal lines. You'll see the warnings/errors listed in detail, and if there was a warning ID it would appear on the line detailing the warning in yellow. At least, thats what I've been told by one of the Compiler Engineers at Apple. I have yet to see a warning that I actually want to disable have an ID appear in the log.

File a bugreport with Apple, tell them you want more options to disable warnings in XCode and list this one specifically.


In Xcode 4.4 this can be disabled by going to the build setting "Other Warning Flags" and setting the value "-Wno-shadow-ivar"


The warning name you're looking for is "shadow-ivar". It appears in the log in Xcode 4.3 as something like warning: local declaration of 'foo' hides instance variable [-Wshadow-ivar].

I still don't see it in the project's Build Settings list, but #pragma clang diagnostic ignored "-Wshadow-ivar" will take care of it in one file at a time. I actually like to turn it off for particular functions and then turn it on again. That way it ceases to nag me where I've decided I don't care but will warn me again in new code.


NSString *foo;

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wshadow-ivar"

- (void)myFunctionWithShadow_ivarWarningsIgnored {

    NSString *foo = @"...";
    NSLog(@"This shouldn't get a warning %@", foo);

}

#pragma clang diagnostic pop

- (void)myFunctionWithShadow_ivarWarningsNotIgnored {

    NSString *foo = @"...";
    NSLog(@"and this should %@", foo);

}

Good Luck! :)


Update for Xcode 8.3 - Bug in compiler yields "Declaration shadows a local variable" in some instances when it intentional... and the nanny panics.

For example in Objective C:

Given

typedef BOOL ( ^BoolBoolBlock ) ( BOOL );

And the nature of Apple Blocks will make any variable declared in the immediate outer scope to a Block, a global to the block (pseudo globals). This results in the warnings (and errors if warnings == errors in your settings) on the line BOOL theResult = false;:

- (BoolBoolBlock) boolBoolBlock {
    BoolBoolBlock    theResult = nil;

    theResult = ^BOOL ( unused BOOL is ) {
        BOOL    theResult = false; // hides (shadows) the outer theResult (a good thing)

        /*
         call back code goes here,
         all variables local in scope to this method are global to the block so be careful
         */
        return theResult;
    };

    return theResult;
}

The nanny sees BoolBoolBlock theResult = nil; getting shadow-blocked by BOOL theResult = false; which is actually intentional for two reasons in this case:

  1. by convention in my code ALL return values are theResult no matter what
  2. is a positive side effects because I am morally opposed to globals.

In other words this entire construct is setup to block the pseudo global mechanisms of Apple Blocks and put structure on that chaos. Blocking the method's "theResult" from use in the block that the method returns is a good thing ... yet the nanny has a hissy fit.

To calm the nanny down (get rid of warnings or possibly errors if you have the discipline to set warnings as errors), you simply make this setting change in your Project File -> Build Settings -> filter on "Other" -> Hidden Local Variables -> change to "No" ... or visually:

How to disable the "local declaration of 'foo' hides instance variable" warning?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号