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

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
going crazy here. can anyone tell me what's wrong with this code? the userdefault has set the Bool to NO, but during the runloop's checkRotate method, the console prints "Rotated", signifying that the bool is yes...

if there is nothing wrong with this code, please comment so at least i'll know for sure, and if there is something wrong with it, i'd love to know what it is!

Code:
//.h
	BOOL didRotate;

//.m
+ (void)initialize
	{
	NSMutableDictionary *userDefaultsDictionary = [NSMutableDictionary dictionary];
	[userDefaultsDictionary setObject:[NSNumber numberWithBool:NO] forKey:@"Rotated"];

	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	[defaults registerDefaults:userDefaultsDictionary];
	[defaults synchronize];
	}

- (void)viewWillAppear:(BOOL)animated
	{
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

	if ([defaults boolForKey:@"Rotated"] == YES)
		didRotate = YES;
		else
		didRotate = !didRotate;

	[self checkRotate];
	[super viewWillAppear:animated];
	}

- (void)checkRotate
	{
	if (didRotate)
		{
		NSLog(@"Rotated");
		}
		else
		{
		NSLog(@"Not Rotated");
		}
	}

- (void)viewWillDisappear:(BOOL)animated
	{
	didRotate = !didRotate;
	[super viewWillDisappear:animated];
	}
 
What are these lines supposed to do?

Code:
	if ([defaults boolForKey:@"Rotated"] == YES)
		didRotate = YES;
		else
		didRotate = !didRotate;
 
what it's suppose to do is read the defaults, if the boolean value for the @"Rotate" key is set to Yes, then to set the didRotate BOOL to yes, if the defaults value is not set to YES, then nil out the didRotate BOOL.

is it not written correctly?
 
What do you mean by 'nil out the didRotate BOOL?'

If didRotate is NO then !didRotate is YES.

ok... so what i mean is that if the @"Rotate" key is set in NSUserDefaults is set to Yes, then have the didRotate BOOL set to yes. if the defaults value is set to NO, then have the didRotate BOOL set to NO.
 
Your description doesn't match the code. I think you want this:

Code:
didRotate = [defaults boolForKey:@"Rotated"];

Is there a place where you write out the current value to the defaults?
 
ok... so what i mean is that if the @"Rotate" key is set in NSUserDefaults is set to Yes, then have the didRotate BOOL set to yes. if the defaults value is set to NO, then have the didRotate BOOL set to NO.

You're not setting it to NO. You're setting it to the opposite of what it is. If it's already NO, it becomes YES. If it's YES, it becomes NO.
 
i think if statements are what confused me...

if i write:
Code:
didRotate = !didRotate

this essentially converts the bool to it's opposite. so a YES will convert to NO, NO will convert to YES.

but where i'm confused is here:
Code:
if (!didRotate)
...

does that equate to "if didRotate is nil or set to NO"? or does it ask "if didRotate is the opposite of its current value"?

my code works if i write "if (!didRotate)", but it doesn't work if i write "if (didRotate = NO)"
 
Code:
didRotate = !didRotate

That is an assing and wil negate didRotate and assign the result of the negating to didRotate. 'flipping' it.

this essentially converts the bool to it's opposite. so a YES will convert to NO, NO will convert to YES.

Correct!


Code:
if (!didRotate)
...
does that equate to "if didRotate is nil or set to NO"? or does it ask "if didRotate is the opposite of its current value"?

Okay, so this is not an assign but an evaluation. so it negates didRotate and if that evaluates to YES the if will be entered. Since the answer is not assigned to anything didRotate is not changed. so no assign. you could also have written:
Code:
if (didRotate == NO) {
...
}

my code works if i write "if (!didRotate)", but it doesn't work if i write "if (didRotate = NO)"


when you write :
Code:
if (didRotate = NO)

the following happens:

First NO is assigned to didRotate and THEN the result is evaluated. Since didRotate is false your if wil never be entered.
 
oh, the blasted "equals equals" (=) vs. "is equal to equals" (==)

*angry fists in the air*

:D

thanks for clearing that up for me... for the millionth time in my life ;)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.