无法访问NSMutableArray中的自定义对象属性

时间:2013-03-28 02:15:58

标签: ios objective-c nsmutablearray

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

我有三个自定义类,表示从机器代码转换的指令类型。每个类都具有类似指令功能和操作数的属性。它们被初始化并转换为第三个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") {
    }

1 个答案:

答案 0 :(得分: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
}