我有一个 Mutable NSArray 存储对象类型(ids - NSNumber,NSString),它将存储数字(1、2、3、4 等)、操作(+、-、/、* 等)和变量( x、y、z 等)。我将变量和相关值存储在 NSDictionary 中,键是等于 x、y、z 的 NSString,NSNumber 值分别为 5、5、2。我想用存储在我的 NSDictionary 中的实际值替换我的 NSArray 中的变量。尝试替换对象时,我不断收到以下错误。请帮忙。
-[__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
}