Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Why NSArray vs. NSMutableArray?

AjaxxAjaxx Posts: 68Registered Users
Hi,
I am developing my first app and is using quite a few arrays to keep data.

I have been thinking about why (and when) to use NSArray over NSMutableArray?

Cheers
Post edited by Ajaxx on

Replies

  • TUX2KTUX2K Posts: 782Registered Users
    - NSArray: final, you cant add/remove item of this array.
    - NSMutableArray: Mutable, you can add/remove item to this array, does take more memory than NSArray.
    If my answer helped you, you might want to help me.

    Make a donation via PayPal.
  • fudgie_no1fudgie_no1 Posts: 45Registered Users
    Ajaxx;280110 said:
    Hi,
    I am developing my first app and is using quite a few arrays to keep data.

    I have been thinking about why (and when) to use NSArray over NSMutableArray?

    Cheers
    Hi Ajaxx, I'm also in the process of creating my first app although coming towards the end now. When you grasp Arrays they are amazing! Someone may correct me but from what I understand...

    NSArrays are immutable (the data inside them can't be changed)
    NSMutableArrays are mutable (the data inside them can be changed)

    So, if you just want to hold data and refer to it - use NSArrays.
    If you want the data to be changable (i.e. using the replaceObjectAtIndex or addObject methods - Use NSMutableArrays.

    hope that helps, if not the search facility will find you a dozen other answers to this I imagine, good luck!
  • baja_yubaja_yu Posts: 6,166Super Moderators, Registered Users
    That's correct. Same can be said of NSString and NSMutableString. But there are also copy and mutableCopy methods for creating both kinds of copies. Just one note about that:

    - making a copy of a mutable string/array will create an immutable string/array

    Other copying works as you might expect.
  • RLScottRLScott Posts: 1,585Tutorial Authors, Registered Users
    I don't think anyone has really answered the OP's question. It is something I have wondered about too. What is the cost of using ..Mutable.. over the non-mutable version? In other words, why not use the ..Mutable.. versions all the time? I know there is more overhead in a mutable object - and probably more memory associated with it. But if the object has a short lifetime, and if there are not too many of them, it is unlikely that the added overhead would impact overall app performance. So, would anyone like to tackle that question?
  • baja_yubaja_yu Posts: 6,166Super Moderators, Registered Users
    That's a good question.

    Being that it's a mobile platform, efficiency and optimization are of utmost importance. It's easy to get get sloppy in the desktop world with GBs or RAM and quad core CPUs working at 3+ GHz. But on a battery operated device with limited resources I think every bit helps.

    Because of the huge resources I think many younger programmers aren't even aware of the terms optimization and efficiency. I've been at many schools/colleges and in most of them those topics aren't covered at all. Long gone are the days of Atari and Commodore 64, where all you had was 64KB or memory and a less than 1MHz CPU and you had to make the best of it. Programmers today are very spoiled.
Sign In or Register to comment.