BOOL Problem?

Discussion in 'iOS Programming' started by Darkroom, Jul 15, 2009.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    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];
    	}
    
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    What are these lines supposed to do?

    Code:
    	if ([defaults boolForKey:@"Rotated"] == YES)
    		didRotate = YES;
    		else
    		didRotate = !didRotate;
    
     
  3. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #3
    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?
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    What do you mean by 'nil out the didRotate BOOL?'

    If didRotate is NO then !didRotate is YES.
     
  5. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #5
    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.
     
  6. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    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?
     
  7. elistan macrumors 6502a

    Joined:
    Jun 30, 2007
    Location:
    Denver/Boulder, CO
    #7
    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.
     
  8. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #8
    oohhhhh! ok, i get it now... ugh... i though !didRotate was totally setting it to NO...

    thanks
     
  9. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #9
    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)"
     
  10. ChOas macrumors regular

    Joined:
    Nov 24, 2006
    Location:
    The Netherlands
    #10
    Code:
    didRotate = !didRotate
    
    That is an assing and wil negate didRotate and assign the result of the negating to didRotate. 'flipping' it.

    Correct!


    Code:
    if (!didRotate)
    ...
    
    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) {
    ...
    }
    

    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.
     
  11. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #11
    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 ;)
     

Share This Page