1

This is my code:

NSString *newString = @"new value"; 


[breakdownCollection objectAtIndex:i] = newString; 

breakdownCollection is an NSArray of multiple strings. I need to access a given string contained in the array via index number, and change the string's content to that of the new string. Note that I cannot simply replace the string with the new one, I am only trying to replace its contents.

When I try to do this, however, I get an "lvalue required as left operand of assignment" error.

Any help with this issue would be very much appreciated!

1
  • Why can't you replace the string with a new one? Commented Jul 9, 2011 at 19:07

2 Answers 2

2

The error you get is because you wrote the assignement instruction incorrectly. That is, you cannot assign newString to [breakdownCollection objectAtIndex:i]. Also, you won't be able to do it this way. Instead, in order to modify string object content, use NSMutableString, which provides methods to do so (NSString are immutable objects). So, for example you should try :

[[breakdownCollection objectAtIndex:i] setString:newString];

assuming you put NSMutableString into breakdownCollection.

PS : in order to change the object at the index i, you have to use NSMutableArray instead of NSArray, and then call :

[breakdownCollection replaceObjectAtIndex:i withObject:newString];

Good luck !

NSMutableString class reference

NSMutableArray class reference

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

Comments

1

Use an NSMutableArray instead and then you can use the method -replaceObjectAtIndex: withObject:

Comments

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.