0

I know this is a really simple thing in obj-c, but I can't seem to find anywhere (on here or google) how to do this. Basically I just want to replace one array value (the value is a NSString) with another, so something like this...

[sharedInstance.groundMap objectAtIndex:ii] = myImage;

But I get an error expression is not assignable. I also tried...

[[sharedInstance.groundMap objectAtIndex:ii] setValue:(NSString*) myImage];

But that gives an error too.

1
  • Really Phil, you couldn't find it anywhere? How about the NSMutableArray Class Reference? I don't understand why so many people look for answers on google or SO, you should go to the Apple docs first. Commented Oct 31, 2012 at 4:38

3 Answers 3

2

First, your array needs to be mutable (i.e. an instance of NSMutableArray, not simply NSArray). With NSMutableArray you can do this:

[sharedInstance.groundMap replaceObjectAtIndex:ii withObject:myImage];

If you have the latest Xcode, you can use the array[index] = value syntax as well.

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

Comments

1

You need to be using an NSMutableArray to set a value. Use the replaceObjectAtIndex:withObject: method or use the new Objective-C syntax:

[sharedInstance.groundMap replaceObjecAtIndex:ii withObject:myImage];

or

sharedInstance.groundMap[ii] = myImage;

Neither works with NSArray. Only NSMutableArray.

Comments

0

This is what you might want

[sharedInstance.groundMap replaceObjectAtIndex:11 withObject:[NSString stringWithFormat:@"test"]];

2 Comments

Why are you using stringWithFormat: for a string literal with no formatting?
it just part of the code i use before since your myImage is an NSString you can use it directly as @maddy and @dasblinkenlight suggested

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.