I’m learning iOS from iTunes U and trying to build a programmable calculator. I have added three variables which the user can use to allot values. I understand that the variables would be passed as NSString objects and then the user would touch the numbers which would be passed as NSNumbers and allotted to the variables. I don’t understand how to use NSArrays and NSDictionary to allot the numbers to these variables. Sorry for being a complete newbie, but I am not comfortable with the Documentation right now, so it is kind of fuzzy! This is my code for appending digits on a UILabel and then using it for calculations:
- (IBAction)digitPressed:(UIButton *)sender {
NSString *digit = [sender currentTitle];
if (self.userIsInTheMiddleOfEnteringANumber) {
self.display.text = [self.display.text stringByAppendingFormat:digit];
everythingBrainLabel.text = [self.everythingBrainLabel.text stringByAppendingFormat:digit];
} else {
self.display.text = digit;
self.everythingBrainLabel.text = [self.everythingBrainLabel.text stringByAppendingFormat:@" %@", digit];
self.userIsInTheMiddleOfEnteringANumber = YES;
}
}
I can get the values of the variables in an NSMutableArray but how to allot numbers here by simply pressing the digits after pressing the variable? I added NSLog just to check if variables were being passed and they show up in the console after I press the respective variable.
- (IBAction)variablePressed:(UIButton *)sender {
NSString *variablePressed = [sender currentTitle];
NSMutableArray *variableValues = [NSMutableArray arrayWithObjects:variablePressed, nil];
for(int i = 0; i < [variableValues count]; i++) {
NSLog(@"%@",[variableValues objectAtIndex:i]);
}
}
To set specific value for specific keys, you could add an NSMutableDictionary to your current class
Add it as a property in your class and synthesize it
Now when you want to assign a value for “x” you would do the following
When assigning the variable “x”
Getting value for “x”