doing the Stanford 193 class courtesy iTunes U. I am trying to copy an NSArray with strings and numbers. I want to replace certain strings with NSDictionary objects. My understanding is that this method would be faster than “replaceObjectAtIndex”
The trouble I have is that nothing seems to be getting copied into stack. The values are present in “program.” The get copied to item. But all values in stack are null. This question is not about retrieving dictionary items. NOTHING is getting copied to the NSMutable array stack regardless of whether or not there are variables to be replaced. I inserted a break point and monitored all variables (program:index, item, stack:index) as it goes through the loop.
What is particularly maddening is that at one point this function WAS working, and now it isn’t.
+(double)runProgram:(id) program
usingVariableValues:(NSDictionary *)variableValues;
{
NSMutableArray *stack;
for (id item in program)
{
if ([item isKindOfClass:[NSNumber class]]) [stack addObject:item];
//above line because comparing numbers to strings throws exception
else if ( ([item isEqualToString:@"x"]) ||
([item isEqualToString:@"a"]) ||
([item isEqualToString:@"b"]) )
{
//if a variable is used, copy the value to program instead of the key
id replacement =[variableValues objectForKey:item];
if (!replacement) replacement = [NSNumber numberWithDouble:0.0];
[stack addObject:replacement];
}
else [stack addObject:item]; //if not a variable, just copy the item
}
//stack should now be a mutable copy of program, but with variables replaced
return [self popOperandOffStack:stack];
} //runProgram
You need to
allocandinityour stack variable.