Advertise here




Advertise here

Howdy, Stranger!

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

NSMutableArray to NSArray

loddy1234loddy1234 Posts: 38Registered Users
edited October 2010 in iPhone SDK Development
how do you convert a mutable array to an array? and vica versa
Post edited by loddy1234 on

Replies

  • smashersmasher Posts: 3,859Registered Users @ @ @ @ @
    edited October 2010
    NSMutableArray *myMutableArray = [myArray mutableCopy];
    

    and
    NSArray *myArray = [myMutableArray copy];
    

    There's not much reason to do the second one since NSMutableArray does everything that NSArray does, unless you expect the mutable array to change and you want a copy of its current state.

    Two things to keep in mind: (1) both of these methods return an object with a retainCount of 1, just like alloc+init does. You own these objects and must release them somewhere. And (2) neither method makes copies of the items themselves, just the pointers. It's like having a list of postal adresses and photocopying the list - that doesn't automagically create duplicate houses. That would be a "deep copy."
    TinyCo is Hiring Mobile Game Programmers (C++)
    http://jobvite.com/m?3Ho5wgwr
  • Duncan CDuncan C Posts: 9,034Tutorial Authors, Registered Users @ @ @ @ @ @ @
    edited October 2010
    smasher wrote: »
    NSMutableArray *myMutableArray = [myArray mutableCopy];
    

    and
    NSArray *myArray = [myMutableArray copy];
    

    There's not much reason to do the second one since NSMutableArray does everything that NSArray does, unless you expect the mutable array to change and you want a copy of its current state.

    Two things to keep in mind: (1) both of these methods return an object with a retainCount of 1, just like alloc+init does. You own these objects and must release them somewhere. And (2) neither method makes copies of the items themselves, just the pointers. It's like having a list of postal adresses and photocopying the list - that doesn't automagically create duplicate houses. That would be a "deep copy."

    Smasher,

    I didn't realize that sending a copy method to a mutable array (e.g. [aMutableArray copy]) would return an immutable copy, but I just looked into it, and you are absolutely right.

    That seems counter-intuitive.
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • smashersmasher Posts: 3,859Registered Users @ @ @ @ @
    edited October 2010
    Duncan C wrote: »
    I didn't realize that sending a copy method to a mutable array (e.g. [aMutableArray copy]) would return an immutable copy, but I just looked into it, and you are absolutely right.

    That's an oddity in Apple's docs that I've never seen explained - whenever a class lists a protocol but has another class in parenthesis, it apparently means that the class only implements the protocol through the superclass. For example, this:
    [B]NSMutableArray Conforms to:[/B]
    NSCoding (NSArray)
    NSCopying (NSArray)
    NSMutableCopying (NSArray)
    NSFastEnumeration (NSArray)
    NSObject (NSObject)
    

    There's important information there - like if you call NSCoding to encode a mutable array, you get an immutable array back when you decode - but only if you know the secret code. Otherwise it's a nasty surprise.

    Ditto for anyone expecting UIImageView to play nice with NSCoding. Maybe Apple explains that somewhere, but if so I missed it.
    That seems counter-intuitive.

    It is, but if it weren't true then "copy" and "mutableCopy" would do the same thing on a mutable array, and you'd need a "immutableCopy" method to get a nonmutable copy; so I can see why they done what they done.
    TinyCo is Hiring Mobile Game Programmers (C++)
    http://jobvite.com/m?3Ho5wgwr
Sign In or Register to comment.