Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Mehrbod Nikbakhsh
293 PointsNSMutableArray & NSDictionary
I'm trying to build a simple App (tableview based) for reading posts (with a "load more button" ) from my back-end (PHP/MySQL) system.
Data I receive is in JSON . like {posts:[{ "title" : "-","time":"-"},{ "title" : "-","time":"-"}]}
I put it in to a NSDictionary
I put the "posts" part of the Dictionary into the NSMutableArray.
the I refresh my tableview
I have problem to add more JSON data to my table. any idea how I can solve this??
thanks
4 Answers
John W
21,558 PointsI assumed your currentPosts was an NSDictionary as you initially described. Here is how the implementation would look like given your property declaration. Notice that now I'm simply referring to what used to be `currentPosts[@"posts"] with the property dot-notation self.posts. Also, the initialization should happen only once, so you should have it in viewDidLoad or lazily in the getter.
/*
In viewDidLoad
*/
self.posts = [NSMutableArray arrayWithCapacity:0];
/*
When you are loading in posts
*/
NSError * error;
// You want to do the following asynchronously, but I'm simplifying here
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://yourserver.com/service.php?q=etc"] ];
NSDictionary * newPosts = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (!error) {
[self.posts addObjectsFromArray:newPosts[@"posts"]];
}
John W
21,558 PointsThe following might work. Haven't tested out myself yet, but basically I would just use addObjectsFromArray: from the NSMutableArray class to add the serialized JSON dictionary's posts element to the currentPosts dictionary's posts element.
// initialize currentPosts as follow or something similar:
NSDictionary * currentPosts = @{ @"posts": [NSMutableArray arrayWithCapacity:0] };
/*
* A little bit down the road...
*/
NSError * error;
// You want to do the following asynchronously, but I'm simplifying here
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://yourserver.com/service.php?q=etc"] ];
NSDictionary * newPosts = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (!error) {
[currentPosts[@"posts"] addObjectsFromArray:newPosts[@"posts"]];
}
Mehrbod Nikbakhsh
293 Pointsthanks a lot !
the code works fine at first, but not when I try to load more data and add to the currentPost. And I think it has to do with my initialization of current posts.
my NSMutableArray is an property in the [.h] file => @property (strong, nonatomic) NSMutableArray *posts; I tried to it into your first line of code , initialize it "NSDictionary * currentPosts = @{ @"posts": [self.posts arrayWithCapacity:0] };"
but it's not correct! how can I fix it?
thanks
Mehrbod Nikbakhsh
293 Pointsfantastic.. and NOW IT WORKS .. finally after 1 weak try and error!
thank you very much John. appreciate it a lot ;)