Re: Setting the value of an array element
Re: Setting the value of an array element
- Subject: Re: Setting the value of an array element
- From: "Sherm Pendley" <email@hidden>
- Date: Thu, 17 Apr 2008 13:19:24 -0400
On Thu, Apr 17, 2008 at 12:15 PM, Johnny Lundy <email@hidden> wrote:
> I imagine this is a very dumb question, but I am perplexed.
>
> NSArray says you can change the values of elements, but not
> add/subtract/replace elements.
>
> Let's say I have an NSArray @"Tom", @"Dick", @"Harry", nil
That's true, but note that the above elements are immutable. That's because
they're literal strings - it has nothing to do with them being in an array.
> And I want to change Tom and Dick to Mutt and Jeff.
You can't change the value of a literal string, regardless of whether it's
in an NSArray or not.
> Is my only option to make it a MutableArray and use replaceObjectAtIndex:0
> and :1 with @"Mutt" and @"Jeff"?
If string literals are a requirement, then yes, that's your only option.
Since you can't modify string literals, your only option is to replace them,
which requires an NSMutableArray.
On the other hand, if using NSMutableString instances is an option, you
could keep the NSArray and modify the elements instead of replacing them:
[[theArray objectAtIndex:0] setString:@"Mutt"];
Is there a way just to set the value of an array element without replacing
> it?
That depends entirely on whether the array element itself is mutable.
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden