On Thu, Apr 17, 2008 at 12:15 PM, Johnny Lundy <[EMAIL PROTECTED]> 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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to