views:

36

answers:

1

When I run this method the two properties I have are set to (NULL) when I try and access them outside of the if statement. But they are set to 0 and NO if I check them inside the if statement.

-(id) init  
{  
    NSLog(@"Jumping into the init method!");  

    if (self = [super init]) {  
        NSLog(@"Running the init method extras");  

        accumulator = 0;  
        NSLog(@"self.accumulator is %g", accumulator);  
        decimal = NO;  
    }  

    NSLog(@"Calc after init is: %@ and %@", self.accumulator, self.decimal);  
    return self;  
}  

Any suggestions as to why what comes out is different from what's done in the for loop?

+4  A: 

The format specifiers are different: you're formatting as %@ (object) in the second case, %g (number) in the first.

(It's just as well they're zero anyway. Otherwise the %@s would probably crash.)

walkytalky
Thanks, I see that works when I make the change. Can you tell me how I access the value of a BOOL, is it just %i as it's represented as an int?
Mark Reid
Yes, you can use %i, though the YES value may be unintuitive. If you're particularly anxious to have something more descriptive you could use %@ in the format string and use (self.decimal?@"YES":@"NO") in the arg list...
walkytalky