0

我在这里遇到了某种结构问题,因为我不确定我正在尝试做的事情是否可行:

我有三个自定义类,它们代表从机器代码翻译的指令类型。每个类都具有指令功能和操作数等属性。它们在第三个 VC 中被初始化并转换为实例并放入 NSMutableArray,我成功地将 NSMutableArray 移动到第四个 VC。现在,我需要遍历数组的每个对象(不知道它是哪种类型的类)并访问它的“指令”属性(即 NSString)。那可能吗?

类之一的声明示例:

@interface iInstruction : NSObject

@property (strong) NSString *opCode;
@property (strong) NSString *rs;
@property (strong) NSString *rt;
@property (strong) NSString *immediate;
@property (strong) NSMutableString *instruction;

- (id) initWithBinary: (NSString *) binaryInstruction;

如何创建实例并移动到第四个 VC:

iInstruction *NewI = [[iInstruction alloc] initWithBinary:binary32Bits];
[TranslatedCode appendFormat:@"%@\n", NewI.instruction];
[instructions addObject: NewI];

disassembledCode.text = TranslatedCode;
FourthViewController *theVCMover = [self.tabBarController.viewControllers objectAtIndex:3];
theVCMover.instructionsInfo = instructions;

我正在尝试做的失败尝试:

for (NSUInteger i=0; i < [instructionsInfo count]; i++) {   //instructionsInfo is a property of the fourth VC that I use to move the main array from the third VC
    NSString *function = [instructionsInfo objectAtIndex:i].instruction;  //Of course it says property not found because it only receives the NSMutableArray at runtime

    if (function isEqualToString:@"andi") {
    }
4

1 回答 1

0

试试这个:

for (iInstruction *instruction in self.instructionsInfo) {
    NSString *function = instruction.instruction;

    // and the rest
}

或者,如果您需要循环计数器:

for (NSUInteger i = 0; i < self.instructionsInfo.count; i++) {
    iInstruction *instruction = self.instructionInfo[i];
    NSString *function = instruction.instruction;

    // and the rest
}

编辑:因为看起来数组可以有不同的对象,但它们都有一个instruction属性,你可以这样做:

for (id obj in self.instructionsInfo) {
    NSString *function = [obj valueForKey:@"instruction"]; // use key-value coding

    // and the rest
}
于 2013-03-28T02:21:43.970 回答