Phil
Phil

Reputation: 3045

Setting a value in an array in obj-c

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.

Upvotes: 0

Views: 3420

Answers (3)

usergio2
usergio2

Reputation: 469

This is what you might want

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

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726609

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.

Upvotes: 2

rmaddy
rmaddy

Reputation: 318824

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.

Upvotes: 1

Related Questions