Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I'm in some kind of structuring trouble here because I'm not sure if what I'm trying to do is possible:

I have three custom classes that represent types of instructions translated from machine code. Each class has properties like the function of the instruction and the operands. They get initialised and translated into instances in the third VC and put in an NSMutableArray and I successfully move that NSMutableArray to the fourth VC. Now, I need to loop through each object of the array (not knowing which type of class it is) and access the "instruction" property of it (an NSString, that is). Is that possible?

Declaration example of one of the classes:

@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;

How the instance is created and moved to fourth 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;

A failed attempt of what I'm trying to do:

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") {
    }
share|improve this question
    
Hi James, you might find this helpful, as if you apply it will ease communication with other Objective-C developers, more people will be willing to read your code an help. best of luck! – Carl Veazey Mar 28 '13 at 2:46
up vote 0 down vote accepted

Try this:

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

    // and the rest
}

or, if you need the loop counter:

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

    // and the rest
}

Edit: Since it appears that the array can have different objects, but they all have an instruction property, you can do this:

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

    // and the rest
}
share|improve this answer
    
That's the problem, it isn't necessarily of the iInstruction class...there are two other classes that it might be of. However, all three have the property of "instruction" as an NSString. – James Baboukis Mar 28 '13 at 2:24
    
See the update I added to my answer. Key-value coding is a neat way to solve this. – rmaddy Mar 28 '13 at 2:27
    
Although a great idea, it gives two errors: "Pointer to a non-const type 'id' with no explicit ownership" and "Bad receiver type '__strong id *" – James Baboukis Mar 28 '13 at 2:34
    
Oops - Get rid of the asterisk in the for loop. See my update. – rmaddy Mar 28 '13 at 2:36
    
Brilliant!!! You, my friend, are a programming artist! I thought I had to completely restructure my code, phew. – James Baboukis Mar 28 '13 at 2:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.