views:

52

answers:

3

Hello, my program normally has a standard background: "black.jpg". In the applicationDidFinishLaunching I did the following:

   [window addSubview:viewController.view];
   [window makeKeyAndVisible];
   viewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"black.jpg"]];

and I have a nice black background. Now I want change my background and for this I made a preference. The defaultValue of the preference is 2, so why does the code not work?:

[window addSubview:viewController.view];        
[window makeKeyAndVisible];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
int bla = [prefs stringForKey:@"background_key"];
NSlog(@"%@", bla) //Console says 2!!
        if (bla == 2) {
            viewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"black.jpg"]];
        }

The view doesn't have the custom background. Thanks for your help!

A: 

1) Hmm, well the %@ qualifier throws me off ... I thought that is reserved for NS* objects. But if your getting 2, that's cool. I'd normally use %d as the qualifier though

2) int bla = [prefs stringForKey:@"background_key"]; Returns a string, not an int ...

3) Your comparison is probably hosed...

Mr-sk
2. No the preference is a NSNumber, so he returns a NSNumber.
Flocked
+4  A: 

edit: see comments for the real reason :)

First use intValue

int bla = [[prefs stringForKey:@"background_key"] intValue];

and then compare.

Adam Woś
Thanks, but it doenst work. I think the compiler doenst get to the if-query...
Flocked
What does `NSLog(@"%@", [bla class])` print out?
Adam Woś
with the intValue: (null), without also
Flocked
Well then, you have your reason - the setting is not set! How are you setting the preference? Using `setInteger:forKey:`? Then you have to read using `integerForKey:`, not `stringForKey:`.
Adam Woś
With this: (backgr is a NSNumber)backgr = [[NSNumber alloc] initWithInt:2]; backgr = [prefs stringForKey:@"background_key"];
Flocked
But this code does not **set** the preference. You have to do something like `[prefs setInteger:2 forKey:@"background_key"]` to save the preference. And then read it by `int bla = [prefs integerForKey:@"background_key"]`. Also, don't forget to `[prefs synchronize]` after you set the setting to save the preferences!
Adam Woś
Thanks, this works :) Sorry, that I have made such a stupid mistake, but Im learning objective-c just for 2 weeks. ;)
Flocked
No problem, glad to help :)
Adam Woś
I would appreciate a checkmark :]
Adam Woś
Flocked -- If this is the correct answer hit the checkmark next to it so that the system knows the questioned has been answered and so that Adam Wos gets credit.
TechZen
Sorry, I know to checkmark the answer, it is the first time that I forgot it ;)
Flocked
No problem - thanks and good luck ;p
Adam Woś
A: 

A unique confluence of problems has confused you:

int bla = [prefs stringForKey:@"background_key"];

bla is now an int value, which holds the address of an NSString object.

NSlog(@"%@", bla) //Console says 2!!

As it should, since %@ says "take the object at the address I'm passing in, and ask for the description". Since the description of the string "2" is "2", you see "2"... If you wanted the format statement to correctly print an int you would need to use "%d", not "%@".

if (bla == 2) {
    viewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"black.jpg"]];
}

But bla is not 2. It's something like 192322, because again it's holding the address of an object, not a pointer to the object.

If you really want an into to compare, use intValue on the result of the prefs call, as others have said. I just thought you might want to understand why the NSLog seemed to mislead you.

Kendall Helmstetter Gelner