通过iTunes U提供Stanford 193课程。我正在尝试使用字符串和数字复制NSArray。我想用NSDictionary对象替换某些字符串。我的理解是这种方法比“replaceObjectAtIndex”
更快我遇到的麻烦是似乎没有任何东西被复制到堆栈中。这些值存在于“程序”中。获取复制到项目。但堆栈中的所有值都为空。这个问题不是关于检索字典项目。无论是否有要替换的变量,NOTHING都会被复制到NSMutable数组堆栈。我插入了一个断点,并在循环中监视所有变量(程序:index,item,stack:index)。
特别令人抓狂的是,这个功能在某个时刻起作用了,现在却没有。
+(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
答案 0 :(得分:0)
您的堆栈变量需要alloc
和init
。
答案 1 :(得分:0)
您没有为堆栈变量分配任何内存,而是尝试将对象添加到nil指针。
NSMutableArray* stack = [[NSMutableArray alloc] init];