我有一个可变的NSArray,存储对象类型(NSNumber、NSString),可以存储数字(1、2、3、4等)、运算符(+、-、/、*等)和变量(x、y、z等)。我已经将变量及其相关值存储在NSDictionary中,键为NSString,分别等于x、y、z,对应的NSNumber值为5、5、2。我想用NSDictionary中存储的实际值替换NSArray中的变量。然而,在尝试替换对象时,我一直收到以下错误。请帮忙解决。
-[__NSArrayI replaceObjectAtIndex:withObject:]: 无法识别的选择子发送给实例
-[__NSArrayI replaceObjectAtIndex:withObject:]: 无法识别的选择子发送给实例
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithDouble:5],@"x",
[NSNumber numberWithDouble:5],@"y",
[NSNumber numberWithDouble:2],@"z",
nil];
+ (double)runProgram:(id)program
usingVariableValues:(NSDictionary *)variableValues;
{
// introspection - ensure program is NSArray and variableValues is NSDictionary
if ([program isKindOfClass:[NSArray class]] && [variableValues isKindOfClass: [NSDictionary class]])
{
// array for program stack
NSMutableArray *stack = [program copy];
// Loop to replace variables with actual values in NSDictionary
for (NSUInteger i = 0; i < [stack count]; i++)
{
NSLog(@"object at index = %@", [stack objectAtIndex:i]);
if ([[stack objectAtIndex:i] isKindOfClass:[NSString class]] && [[stack objectAtIndex:i] isEqualToString: @"x"])
{
// replace variable with value in corresponding value in dictionary
NSNumber *numberKeyValue = [variableValues objectForKey:@"x"];
[stack replaceObjectAtIndex:i withObject:numberKeyValue];
}
}
return 0
}