I’m teaching myself iOS 5 programming through the Stamford University CS193P class, I just started and am on the first project. I want to echo values to the console using NSLog(), and any logs I send to the console from within my controller show up, however any NSLog messages sent from the model class do not. Am I doing something wrong?
From my CalculatorViewController.m file:
- (IBAction)enterPressed
{
//The Following NSLog() works.
NSLog(@"Adding number %g to operandStack", [self.display.text doubleValue]);
[self.brain pushOperand:[self.display.text doubleValue]];
self.userIsInTheMiddleOfEnteringANumber = NO;
}
From my CalculatorBrain.m file:
-(void)pushOperand:(double)operand
{
//The Following NSLog() doesn't show up in the console
NSLog(@"Received number %@ for operandStack", operand);
NSNumber *operandObject = [NSNumber numberWithDouble:operand];
[self.operandStack addObject:operandObject];
}
The code isn’t working correctly either, everything compiles and runs just fine, no warnings or errors, but the methods in the model aren’t returning the expected objects, they’re (nil), so I feel like maybe the way the controller is interacting with the model is wrong?
This is how I’m instantiating the CalculatorBrain model BTW:
#import "CalculatorViewController.h"
#import "CalculatorBrain.h"
@interface CalculatorViewController()
@property (nonatomic) BOOL userIsInTheMiddleOfEnteringANumber;
@property (nonatomic, strong) CalculatorBrain *brain;
@end
@implementation CalculatorViewController
@synthesize display = _display;
@synthesize userIsInTheMiddleOfEnteringANumber = _userIsInTheMiddleOfEnteringANumber;
@synthesize brain = _brain;
-(CalculatorBrain *)brian
{
if (! _brain) _brain = [[CalculatorBrain alloc] init];
return _brain;
}
There’s a typo in your getter for
brain. When you messageself.brain, it’s always going to return nil because the mistyped method,brian(where you do your instantiation), isn’t being messaged.Also the format specifier for a double is
%f. So your NSLog should look like this…