Objective-C newbie here, working on a programming calculator for iOS5.
Everything works fine with regular old digits:
- (void)pushOperand:(double)operand
{
[self.programStack addObject:[NSNumber numberWithDouble:operand]];
}
But when I create a dictionary with test numbers for variables, like so:
- (void) pushOperandAsVariable:(NSObject *)variable
{
//Create dictionary
NSArray *objects = [[NSArray alloc] initWithObjects:[NSNumber numberWithDouble:3],[NSNumber numberWithDouble:4.1],[NSNumber numberWithDouble:-6],[NSNumber numberWithDouble:4.5298], nil];
NSArray *keys = [[NSArray alloc] initWithObjects:@"x",@"y",@"z",@"foo", nil];
NSDictionary *variableValues = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];
NSNumber *operand;
//Check variableValues for keys
for (int i=0; i<keys.count; i++)
{
if ([keys objectAtIndex:i]==variable)
operand = [variableValues objectForKey:variable];
}
NSLog(@"%@, %@",variable,operand);
[self.stackOfThingsSinceLastClear addObject:operand];
}
The NSLog in the penultimate line always comes up “x, (null)”. What am I missing, that my operands are disappearing?
There is no
==operator defined for objects (it would compare the pointer values).Use the
isEqual:method.