Warning: local declaration of x hides instance variable

Discussion in 'iOS Programming' started by Ralle, Feb 9, 2009.

  1. Ralle macrumors newbie

    Joined:
    Jan 6, 2009
    #1
    So I have been searching in a few hours for why my iPhone app hates me. This is the error I get:
    Warning: local declaration of 'searchBox' hides instance variable.
    Here is my AppDelegate.m
    Code:
    #import "RejseAppDelegate.h"
    
    @implementation RejseAppDelegate
    
    @synthesize window;
    
    
    - (void)applicationDidFinishLaunching:(UIApplication *)application {    
    
        // Override point for customization after application launch
        [window makeKeyAndVisible];
    }
    
    - (void)searchBarSearchButtonClicked:searchBox;
    {
    	Warning: local declaration of 'searchBox' hides instance variable.
    	NSString* sbc = [searchBox text];
    	[navBar setTitle:sbc];
    	
    	NSString* purl = @"a";
    	NSString* ppurl = [purl stringByAppendingString:sbc];
    	
    	NSString* url = [ppurl stringByAppendingString:@"b"];
    	
    	[ppurl release];
    	[purl release];
    	
    	NSLog(url);
    	
    	[url release];
    	[sbc release];
    }
    
    - (void)dealloc {
        [window release];
        [super dealloc];
    }
    
    
    @end
    
    AppDelegate.h
    Code:
    #import <UIKit/UIKit.h>
    
    @interface RejseAppDelegate : NSObject <UIApplicationDelegate> {
        UIWindow *window;
    	IBOutlet UITableView *resultList;
    	IBOutlet UISearchBar *searchBox;
    	IBOutlet UINavigationItem *navBar;
    }
    
    @property (nonatomic, retain) IBOutlet UIWindow *window;
    
    - (void)searchBarSearchButtonClicked:searchBox;
    
    @end
    I really don't understand why it says that I am hiding the instance variable.
     
  2. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #2
    It is saying that the searchBox parameter that is part of your searchBarSearchButtonClicked method makes it impossible to access your class' instance variable with the same name in that method. As long as you only have one search box it won't really matter, but you can change the name of either searchBox to get rid of the warning.
     
  3. plinden macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #3
    Is this supposed to be a method declaration?
    Code:
    [B]- (void)searchBarSearchButtonClicked:searchBox;[/B]
    {
    	Warning: local declaration of 'searchBox' hides instance variable.
    	NSString* sbc = [searchBox text];
    	[navBar setTitle:sbc];
    	
    	...
    }
     
  4. Ralle thread starter macrumors newbie

    Joined:
    Jan 6, 2009
    #4
    Well I copied the event from the apple documentation and I am not 100% sure about how everything works.
    I would like to access the searchBox's text somehow and I dunno which variable to rename for it to still work.

    The event is from a searchBar and I want the function to execute whenever the 'Search' button is clicked.

    This is how the app will look:
    [​IMG]

    EDIT:
    Okay so I have changed this:
    - (void)searchBarSearchButtonClicked:searchBox;
    to this:
    - (void)searchBarSearchButtonClicked:searchBoxt;
    which apparently works.

    Thanks!
     
  5. caveman_uk Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #5
    The problem is because you have an class instance variable called searchBox - which is connected to an object on your nib. When you declare the method.
    Code:
    - (void)searchBarSearchButtonClicked:searchBox;
    
    which is actually incorrect as it should be
    Code:
    - (void)searchBarSearchButtonClicked:(UISearchBar*)searchBox;
    
    you are creating a completely different variable called searchBox which could be a pointer to something completely different.

    Incidently, I very much doubt you copied this off Apple's website as your code is riddled with errors. I strongly advise you look into memory management as you're trying to release a load of stuff that's already autoreleased. Your app will crash.
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    I agree. Plus, based on the root of the OP's question, they don't seem to have a basic understanding of method declarations and parameters. I would suggest before going any further that it's time to step back and learn some Objective-C fundamentals.
     

Share This Page