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-01-2010, 10:50 AM   #1 (permalink)
Registered Member
 
Wunk's Avatar
 
Join Date: Nov 2008
Location: Netherlands
Posts: 28
Wunk is on a distinguished road
Send a message via ICQ to Wunk
Default How to fill this NSDictionary into a UITableView

I'm parsing a piece of JSON into a NSDictionary, but I'm having some trouble sequencing this into an array of data which I can push into a UITableView

After parsing the JSON data I have this in a NSDictionary (this is the result from NSLog on the NSdictionary):

Code:
    1 =     {
        artist = "Bruno Mars";
        "artist_url" = "http://url.here/bruno-mars.htm";
        attributes =         {
            youtube = "http://www.youtube.com/v/7pNDpxkf3hk";
        };
        "lyric_url" = "http://url.here/just-the-way-you-are.htm";
        title = "Just the way you are";
    };
    2 =     {
        artist = "Duck sauce";
        "artist_url" = "http://url.here/duck-sauce.htm";
        attributes =         {
            youtube = "http://www.youtube.com/v/wWhtcU4-xAM";
        };
        "lyric_url" = "http://url.here/barbra-streisand.htm";
        title = "Barbra Streisand";
    };
etc.. until I have 40 results..

Basically what I want to do, is:

- Grab the results from 1 and stuff the variables into an array
- Parse the array (no issue here, once I have it actually IN there I can work with it no problem)
- Stick the results from 1 into the first indexPath.row entry of the UITableView (that'd be index 0)
- recycle the array, rince and repeat for entry 2 to 40

I could use some pointers for especially the first problem, specifically I need to grab JUST 1 entry from the NSDictionary and stuff it into an array..
Wunk is offline   Reply With Quote
Old 12-01-2010, 11:04 AM   #2 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 60
substance21 is on a distinguished road
Default

You just need to iterate over the keys of the dictionary. However, dictionary keys are unordered so you need to make sure they're sorted in the order you expect. You can do something like:

Code:
NSArray *keys=[[dictionary allKeys] sortedArrayUsingSelector:@selector(compare:)];


for (NSNumber *key in keys) {
   NSDictionary *data=[dictionary objectForKey:key];
   //data should contain artist, artist_url, etc....
}
The above should work if your keys are NSNumbers (which it looks like from the json dump).
__________________
iPhoneApps:
enLegion - When everyone needs to be in the same place, but isn't
AutoWeb - Automate your web logins and browsing!

Last edited by substance21; 12-01-2010 at 11:08 AM.
substance21 is offline   Reply With Quote
Old 12-01-2010, 11:25 AM   #3 (permalink)
Registered Member
 
Wunk's Avatar
 
Join Date: Nov 2008
Location: Netherlands
Posts: 28
Wunk is on a distinguished road
Send a message via ICQ to Wunk
Default

Quote:
Originally Posted by substance21 View Post
The above should work if your keys are NSNumbers (which it looks like from the json dump).

Doesn't seem to work, once I do (where self.jsonResult is the NSDictionary with data):

Code:
NSArray *keys=[self.jsonResult keysSortedByValueUsingSelector:@selector(compare:)];
It crashes with:

2010-12-01 18:21:45.661 MyApp[12481:207] -[__NSCFDictionary compare:]: unrecognized selector sent to instance 0x5d4a8c0


edit Actually never mind, either I misread or you changed something in the post, because once I altered it to NSArray *keys=[[self.jsonResult allKeys] sortedArrayUsingSelector:@selector(compare]; it worked

Thanks

Last edited by Wunk; 12-01-2010 at 11:45 AM.
Wunk is offline   Reply With Quote
Old 12-04-2010, 10:42 AM   #4 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 60
substance21 is on a distinguished road
Default

Ya, I did edit my post. I mistakenly used keysSortedByValueUsingSelector which sorts by the value instead of the key.

Glad it worked.
__________________
iPhoneApps:
enLegion - When everyone needs to be in the same place, but isn't
AutoWeb - Automate your web logins and browsing!
substance21 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



» Advertisements
» Online Users: 368
6 members and 362 guests
daudrizek, HemiMG, Kirkout, MarkC, Sami Gh
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,665
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, daudrizek
Powered by vBadvanced CMPS v3.1.0

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