PDA

View Full Version : Calculation syntax problem using application settings




boyplunder
Jul 12, 2011, 03:57 AM
Hi all,

I have set up application settings for a project and created the same settings as a view in the app. All this works a treat. I am now trying to add a calculation that reads one of the settings and displays an image based on whether the setting is on or off.

Currently, my code is this:

//Check the map to use
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL enabled = [defaults boolForKey:@"maptype"];

if (enabled) {
return (UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"map2.jpg"]]);
} else {
return (UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"map1.jpg"]]);
}
self.imageView = tempImageView;

I get an error for each of the 'return' lines: Expected ")" before 'tempImageView'. The last line also says it's not declared, but this probably relates to the first issue.

I have been through it many times and am probably not seeing the wood for the trees, as they say. Could anyone give me a hand in understanding what the issue is?



robbieduncan
Jul 12, 2011, 04:32 AM
You cannot declare a new variable within a return call like that. It serves no purpose. Return will exit the current method so all local variables will go out of scope. In addition your like setting self.imageView can never be reached due to the return statements.

boyplunder
Jul 12, 2011, 08:50 AM
Appreciated.
It's pretty obvious what I am trying to do, I think, I just have to understand the right method of doing it for myself. From what you say I have to deal with the variables outside of the calculation, which is why the self.ImageView would never work, as there will never be a value to play with. Or not use a variable for this...

Aahh, the joy of learning!

robbieduncan
Jul 12, 2011, 08:53 AM
From what you say I have to deal with the variables outside of the calculation, which is why the self.ImageView would never work,

No, the self.imageView statement will never even get executed. This is very different from it not working. return exits the method right there and then. Any statements after it simply do not get executed. return does not flag a variable as the value you will return or anything like that.

I would suggest you need to learn very basic C as this is a pretty fundamental, core error.