PDA

View Full Version : Local Declaration of '' hides instance variable




lutico
Jan 24, 2010, 05:13 PM
Hi guys,

I'm new at iphone sdk, and experimenting some problems with variables.

What I'm trying to do is load an Array from a plist file, and show a random String in a WebView. And when a button is pressed it gets other random string from the array.

In the simulator, when the program loads it shows a string, but when I press the button, the program shuts down.

Here's the plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<string>ssssssssssssss</string>
<string>llllllllllllllllllllllllll</string>
<string>okokokok</string>
<string>ljdcosijc</string>
<string>kwhcoiwkjscb</string>
<string>ksjjjjjjjjjjjj</string>
</array>
</plist>


When view loads
I'm getting "Local declarations of 'getText' hides instance variable" on the red text

- (void)viewDidLoad {
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"Quotes.plist"];
NSMutableArray *allText = [[NSMutableArray alloc] initWithContentsOfFile:finalPath];
NSString *getText = [[NSString alloc] init];
getText = [allText objectAtIndex:(arc4random() % [allText count])];
email = [[NSString alloc] initWithString:getText];
texto.backgroundColor = [UIColor clearColor];
[texto loadHTMLString:getText baseURL:nil];
}


When the button is pressed:

-(IBAction)mostraTexto{
[getText release];
getText = [allText objectAtIndex:(arc4random() % [allText count])];
[email release];
email= [NSString stringWithString: getText];
[texto loadHTMLString:getText baseURL:nil];
}

Thanks in advance!

Regards
Ruben Oliveira



xsmasher
Jan 24, 2010, 06:11 PM
"Local Declaration of XYZ hides instance variable" means that you have an instance variable called getText declared in your .h file, and you declared an local variable (a variable declared inside a method) with the same name.

Either change the name of the local variable, or don't declare the local variable at all if you don't need it. You probably have the same problem with allText too - you should not declare it inside the method if you need to access it from the whole object.

You have some other errors too -

//delete this line
NSString *getText = [[NSString alloc] init];


It's almost never useful to create an empty string like this, since strings are immutable and can't be changed. On the very next line you point getText at a different string anyway, so this empty string gets leaked.

The method mostraTexto also has three errors- you're releasing the "getText" string that you didn't retain or alloc, you're trying to access the instance variable allText even though you put the array into the local variable, not the instance variable. And you're releasing the "email" string that you did init - but the second time you click the button it will crash because email is pointing to an autoreleased string.

It sounds like you have problems with instance variables vs. local (automatic) variables, memory management (retain/release), and possibly pointers. This code should work without crashing, although it may not do what you want:


- (void)viewDidLoad {
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"Quotes.plist"];
allText = [[NSMutableArray alloc] initWithContentsOfFile:finalPath];
getText = [allText objectAtIndexarc4random() % [allText count])];
email = getText;
texto.backgroundColor = [UIColor clearColor];
[texto loadHTMLString:getText baseURL:nil];
}

When the button is pressed:
-(IBAction)mostraTexto{
getText = [allText objectAtIndexarc4random() % [allText count])];
email= getText;
[texto loadHTMLString:getText baseURL:nil];
}


I couldn't figure out the purpose of "email" if it holds the same string as "getText." Also I never retained the strings in this code, so if you release allText then the strings will be destroyed too.

lutico
Jan 24, 2010, 07:08 PM
It's working great now!
I guess I'll have to take a deep look into variables definitions... :o

Thank you very much!