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 07-10-2008, 11:42 AM   #1 (permalink)
Registered Member
 
tawpie's Avatar
 
Join Date: Jul 2008
Posts: 347
tawpie is an unknown quantity at this point
Default another why: NSDictionary or NSArray (mutable or not)

OK, I think I understand the key-value part of NSDictionary... NSDictionary at its simplistic core to be another way to deal with an NSArray, which is of course an objectified C array. Is it a matter of programming style/preference to choose NSDictionary over NSArray, or are there specific situations or desired end goals that would influence one to chose one over the other? For instance, when dealing with external "files" or application preferences, the Apple example code likes to have things saved as dictionaries rather than say, character arrays or NSStrings. info.plist contains dictionary objects true, but is this because it's "better", or just because "that's the way it is". Would I be crazy to save non-plist data as an NSArray directly (not info.plist data, but something else like the type of wireless connection in operation during each of the last 10 visits to a particular website -- something for which I don't need the key-value pairing)?

Perhaps it would help me most for your ideas on a list of "dictionaries are good for this, that and this other thing" but if you want to do things like "thisListOfOtherStuff" then NSArray is better... BECAUSE:
__________________
"Hardware will break. Software comes broken" Unknown
Calc-12E <-- ditch your old calculator.
CPR•Choking <-- Review your training, just in case. (Free)
All of Nature NW <-- scrolls in x and y, with pinch zoom AND scrollable text to boot. It can be done ('taint easy tho)
tawpie is offline   Reply With Quote
Old 07-10-2008, 02:06 PM   #2 (permalink)
Registered Member
 
Join Date: Apr 2008
Posts: 339
Delirium is an unknown quantity at this point
Default Re: another why: NSDictionary or NSArray (mutable or not)

I've been using dictionaries when I want to have a list of associations of one object to another. For instance, if you wanted to keep track of which touch activated which subview on the screen, you'd have a dictionary list of UITouch and UIView objects, where one is the key and one is the value.
Delirium is offline   Reply With Quote
Old 07-10-2008, 11:39 PM   #3 (permalink)
Registered Member
 
tawpie's Avatar
 
Join Date: Jul 2008
Posts: 347
tawpie is an unknown quantity at this point
Default Re: another why: NSDictionary or NSArray (mutable or not)

so perhaps I should think of a dictionary as an array of objects and think of the dictionary's "key" in the same way as I think of an array's "index"'? And then an NSArray (which holds objects too) is...???
__________________
"Hardware will break. Software comes broken" Unknown
Calc-12E <-- ditch your old calculator.
CPR•Choking <-- Review your training, just in case. (Free)
All of Nature NW <-- scrolls in x and y, with pinch zoom AND scrollable text to boot. It can be done ('taint easy tho)
tawpie is offline   Reply With Quote
Old 07-11-2008, 09:26 AM   #4 (permalink)
Registered Member
 
Join Date: Apr 2008
Posts: 93
BuschyBoy is an unknown quantity at this point
Default Re: another why: NSDictionary or NSArray (mutable or not)

The main distinction that often picks why I pick each one is that you can name items inside of a dictionary (Give them a key) where as an array simply has an index.
BuschyBoy is offline   Reply With Quote
Old 07-11-2008, 10:55 AM   #5 (permalink)
Registered Member
 
Join Date: Apr 2008
Posts: 339
Delirium is an unknown quantity at this point
Default Re: another why: NSDictionary or NSArray (mutable or not)

I would use an NSArray when you don't need direct access to a specific item, and are just interested in having a list of same typed objects to loop through.
Delirium is offline   Reply With Quote
Old 07-11-2008, 11:58 AM   #6 (permalink)
Registered Member
 
tawpie's Avatar
 
Join Date: Jul 2008
Posts: 347
tawpie is an unknown quantity at this point
Default Re: another why: NSDictionary or NSArray (mutable or not)

thank you to all, this is quite helpful!
__________________
"Hardware will break. Software comes broken" Unknown
Calc-12E <-- ditch your old calculator.
CPR•Choking <-- Review your training, just in case. (Free)
All of Nature NW <-- scrolls in x and y, with pinch zoom AND scrollable text to boot. It can be done ('taint easy tho)
tawpie is offline   Reply With Quote
Old 07-11-2008, 12:42 PM   #7 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default Re: another why: NSDictionary or NSArray (mutable or not)

NSArray is more appropriate when you have a bunch of objects that are all the same type, and you're likely to loop through them in order. For example, a list of books could be put in an NSArray- book[0] is one book, book[1] is another book.

NSDictionary is more appropriate when you have objects (sometimes of different types) that you want to access by keys; often out of order. For example, an individual book could be represented as a Dictionary, where the key "sold" refers to a NSNumber for the number sold, and the key "Title" points to an NSString for the title of the book.

Note that I said "most appropriate," not "only used." You could "abuse" NSArray by putting objects of different types in it, and remember that index 0=number sold and index 1=title, but when you try to add a title without adding the number sold first, you'll see the wisdom.

In my own application a list of workouts is an NSMutableArray, each workout in the array is an NSDictionary, and and the key "exList" in that dictionary points to an NSArray full of exercises.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 07-11-2008, 01:42 PM   #8 (permalink)
Administrator
 
Chris Stewart's Avatar
 
Join Date: Mar 2008
Location: Richmond, VA
Age: 29
Posts: 743
Chris Stewart is an unknown quantity at this point
Default Re: another why: NSDictionary or NSArray (mutable or not)

I find it helpful to understand the details of the respective data structures in determining which to use for a problem. A dictionary is essentially a hashtable. The benefits of a hashtable is in the insertion, deletion, and search. The Big O notation for a hashtable is described as O(1), which is constant time and is the fastest. Because of the hashing nature of the keys, it's a 1 to 1 mapping between a key and a memory location. So there's no searching up and down segments of memory. Hence, the O(1) constant time evaluation. The drawback to a hashtable is the random nature in which the data stored is distributed in memory. The various nodes are not stored contiguously in memory. To the developer, that means it's not a data structure that benefits from looping through the nodes one by one.

An array, at it's core, is a very inefficient data structure because most people use a data structure by adding nodes to it. It's major benefit is that the nodes are stored contiguously in memory, so going from node to node in sequence is easy and fast. Adding elements to an array is a seriously expensive process because essentially the memory is allocated for the array each time a new node is added. As an example, if you start with 0 nodes, your array is empty and using "no" memory. Add a node and a new array is created, the old array is copied in, and your new node is added to the end. Repeat this process for all nodes added. You can quickly see this problem grows exponentially, which drastically effects it's Big O notation, and describes the structure as being slow.

Of course, this is the fundamental stuff. Real world scenarios determine which structures are used. Use which is best for your needs.
__________________
Founder, Locomo Labs, LLC
SocialBlast -- iPhone/iPad -- Update Twitter/Facebook/LinkedIn/Foursquare

Previous owner, and original founder, of iPhone Dev SDK
Chris Stewart is offline   Reply With Quote
Old 07-11-2008, 10:22 PM   #9 (permalink)
Registered Member
 
tawpie's Avatar
 
Join Date: Jul 2008
Posts: 347
tawpie is an unknown quantity at this point
Default Re: another why: NSDictionary or NSArray (mutable or not)

bingo! I get hash tables and all the complexity and beauty they bring, thank you for equating the dictionary as such. Makes things very clear as to "why".

It also appears that with mutable dictionaries, one can be pretty casual when adding entries... I need to look again carefully but it looks a lot like you just setObject:forKey and you're off and running. (more care must be taken when removing entries though!) It also appears that the ability to write a dictionary easily is kind of nice, although there is a warning that the plist format isn't for general persistent storage -- do plists get clobbered somehow?
__________________
"Hardware will break. Software comes broken" Unknown
Calc-12E <-- ditch your old calculator.
CPR•Choking <-- Review your training, just in case. (Free)
All of Nature NW <-- scrolls in x and y, with pinch zoom AND scrollable text to boot. It can be done ('taint easy tho)
tawpie is offline   Reply With Quote
Reply

Bookmarks

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating an NSArray out of a property list? BuschyBoy iPhone SDK Development 4 05-28-2008 11:09 AM


» Advertisements
» Online Users: 321
7 members and 314 guests
blueorb, guusleijsten, jbro, Kryckter, mer10, n00b, SLIC
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,880
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

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