0

OK. I've spent a ridiculous amount of time on this. I've looked all around on the internet to the point of feeling pretty stupid. I have program which is an array of x, 2, "+". So I make a copy of program and try to replace x with its value (5) from variableValues.

Can anyone tell me how in the world to do this?

+ (double)runProgram:(id)program usingVaraiableValues:(NSDictionary *)variableValues {

    NSMutableArray *program2 = program;
    for (NSString *obj in program) {
        NSNumber *number = [variableValues objectForKey:obj];
        if (number) {
            int index = [program indexOfObject:obj];
            NSLog(@"index = %i", index);
            [program2 replaceObjectAtIndex:index withObject:number];
        }
    }
    return [self runProgram:program2];
}
1
  • 1
    program2 = program doesn't make a copy, much less a mutable one. Commented Jul 5, 2013 at 3:30

1 Answer 1

1

Actually program2 isn't a copy of program, it is a reference to the same memory address. Try doing NSMutableArray *program2 = [program mutableCopy]; instead.

Sign up to request clarification or add additional context in comments.

5 Comments

This probably won't work since program is of type id. :-P
Ok, maybe, but not probably. Furthermore, if program isn't a NSArray, this line probably won't work too int index = [program indexOfObject:obj];.
Yeah, the problem though, is that the compiler won't recognize the id as accepting the message mutableCopy. It needs to be cast to an NSArray...
Or better yet, just change the signature of the method so that it requires an array. That way we always know what we are getting. :)
Thank you, guys. I wish I could add points. You both are great!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.