Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-29-2010, 05:26 AM   #1 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 67
Ajaxx is on a distinguished road
Question Why NSArray vs. NSMutableArray?

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
Ajaxx is offline   Reply With Quote
Old 12-29-2010, 05:39 AM   #2 (permalink)
Registered Member
 
Join Date: Oct 2009
Location: Amsterdam, The Netherlands
Posts: 782
TUX2K is on a distinguished road
Default

- 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.
TUX2K is offline   Reply With Quote
Old 12-29-2010, 05:45 AM   #3 (permalink)
Creator of From A to B
 
fudgie_no1's Avatar
 
Join Date: Nov 2010
Location: Wales
Posts: 45
fudgie_no1 is on a distinguished road
Default

Quote:
Originally Posted by Ajaxx View Post
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!
fudgie_no1 is offline   Reply With Quote
Old 12-29-2010, 07:30 AM   #4 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

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.
baja_yu is offline   Reply With Quote
Old 12-29-2010, 07:50 AM   #5 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,549
RLScott is on a distinguished road
Default

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?
RLScott is offline   Reply With Quote
Old 12-29-2010, 07:55 AM   #6 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

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.
baja_yu is offline   Reply With Quote
Reply

Bookmarks

Tags
nsarray, nsmutable array, objective-c

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 358
9 members and 349 guests
akphyo, blueorb, fredidf, iAppDeveloper, iGamesDev, Kirkout, MarkC, mottdog, Touchmint
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,667
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, host number one
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:27 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0