I’m trying to make a simple calculator application in cocoa. The program hangs when I click on one of my buttons. I think I’ve traced the problem to the part of my controller that adds a digit to the end of the number currently on the display:
- (void)updateNumber:(int)buttonClicked{
*self.activeNumberPointer = *self.activeNumberPointer * 10 + buttonClicked;
[outputField setFloatValue:*self.activeNumberPointer];
}
I used a pointer to the “activeNumber” in order to allow my program to tell which of the two operands I’m editing.
Any help appreciated, thanks.
(edit): My declaration and @property:
//CalculatorController.h
@interface CalculatorController : NSObject {
IBOutlet NSTextField *outputField;
float variable1, variable2;
float *variable1Pointer, *variable2Pointer;
float *activeNumberPointer;
}
float variable1 = 0;
float variable2 = 0;
float* variable1Pointer = &variable1;
float* variable2Pointer = &variable2;
float* activeNumberPointer = &variable1;
@property (readwrite) float variable1, variable2;
@property (readwrite) float *vairable1Pointer, *variable2Pointer;
@property (readwrite) float *activeNumberPointer;
...
Full XCode file available here: http://rapidshare.com/files/397664243/Calculator_2.zip
(FYI: I actually used leftNumberValue and rightNumberValue instead of variable1 and variable2 in the project)
Since I’m new to Objective-C and XCode, any general criticisms are welcome.
Not clear on pointers in objective-C with simple variable types, and not totally clear why you’re using pointers at all.
You are using the * operator (dereference) wrong on the left side of this expression. When doing assignment, you don’t want to dereference like that.
http://www.cplusplus.com/doc/tutorial/pointers/
Thats a C++ reference, but a good start for pointer logic.
I would probably use an NSMutableArray or some construct like that (you want to store more than just two numbers right?), and use the integer index to point to the “current” number you’re displaying on the screen.
Further, I don’t know if its a typo, or some missing code etc, but…
… you seem to be declaring all the variables above in the interface definition as ivars — or instance variables that are directly attached to your CalculatorController, and then outside of the interface definition, you are redeclaring these as GLOBAL variables. There’s no need for the second set of definitions to use these in your implementation
If you are looking to initialize these instance variables, commonly there is a function called -init that you can override (since you are subclassing NSObject), and its akin to a constructor in other languages. This would live in your implementation file (.m or .mm). It might look something like this:
There’s also another function -awakeFromNib that you can override if this is being instantiate inside a XIB/NIB file as part of your user interface definition. The -awakeFromNib function gets called when the class and the UI have been loaded.
Edit: to be clear about the pointer business….
self.activeNumberPointer = *self.activeNumberPointer * 10 + buttonClicked;
This is right. You DON’T use the dereference operator on the left hand side, but you DO use the dereference operator on the right hand side, as you are looking for the value of self.activeNumberPointer.
Josh