Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

boyplunder

macrumors regular
Original poster
Sep 8, 2008
165
0
UK
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:

Code:
//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?
 
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.
 
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!
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.