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.

4

3 回答 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.

于 2012-10-31T04:06:51.977 回答
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.

于 2012-10-31T04:06:30.890 回答
0

This is what you might want

[sharedInstance.groundMap replaceObjectAtIndex:11 withObject:[NSString stringWithFormat:@"test"]];
于 2012-10-31T04:08:06.220 回答