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!
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

Andrew Barber
3,865 PointsLoading JSON into IOS Application
Hi All,
I seem to be having trouble with loading the id value which is displayed within my iphone application.
When I run the app, it loads the the name, price and a and an image URL. The only section which fails to load depending on its value is the 'id'. Is anyone able to tell me why this happens just by looking at this code?
- (void) viewWillAppear:(BOOL)animated {
NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://hostname/TM.php?id=%@",/*self.scannedValue */]]];
//NSData *jsonData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Sample" ofType:@"JSON"]];
NSArray *jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:Nil];
NSDictionary *objectsDict = [jsonObject objectAtIndex:0];
[self.nameLabel setText:[NSString stringWithFormat:@"Name: %@",[objectsDict objectForKey:@"name"]]];
[self.priceLabel setText:[NSString stringWithFormat:@"Price: %@",[objectsDict objectForKey:@"price"]]];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[objectsDict objectForKey:@"image_url"]]];
[self.imageView setImage:[UIImage imageWithData:imageData]];
}
Many Thanks.

Andrew Barber
3,865 PointsStone Preston Yep, thats the line.
When I change %@ to an integer value it shows the image and other info for that id number. but I'm trying to get that part of the code to take any id number.
I set self scannedValue in my scannedViewController.m

Stone Preston
42,016 Pointsok can you elaborate more on what you mean by "trying to get that part of the code to take any id number"

Andrew Barber
3,865 Pointslocalhost/TM.php?id=101 <<-- displays data about one item localhost/TM.php?id=102 <<---- displays data about a different item localhost/TM.php?id=103 <<---- displays data about a different item etc..
I want to replace the '%@' with something that can take in any chosen id number. Just not too sure how to do that.
2 Answers

Andrew Barber
3,865 Points.

Stone Preston
42,016 Pointshave you tried using %d in place of %@? i havent worked much with JSON so i cant be of much help but if your JSON data for ID is an integer, then you probably need to use %d instead of %@. But I could be completely wrong haha.

Andrew Barber
3,865 PointsI tried it but sadly it didnt work.
Its definetly something in this line which is wrong:
NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://localhost/TM.php?id=105",self.scannedValue]]];
I get a yellow message appear saying 'data argument not used by format string' underneath 'self.....
Not too sure what format I should be using in replacement.

Stone Preston
42,016 Pointsthats because you dont have a format specifier in your string, but pass in an argument to stringWithFormat
[NSString stringWithFormat:@"http://localhost/TM.php?id=105",self.scannedValue]
to fix it you would have to put a format specifier in place of 105
you need a format specifier in your string somewhere. Of course the type for specifier depends on the type of your scanned value property. What is the type of that property? can you post some code where you set that property to a value?

Andrew Barber
3,865 PointsSorry for the late reply.
I tried the following specifer before I posted in this forum a while ago but I couldnt get it to work:
[NSString stringWithFormat:@"http://localhost/TM.php?id="%@",self.scannedValue]
The property type for scannedValue is of NSString.
@property (nonatomic, retain) NSString *scannedValue;
Stone Preston
42,016 PointsStone Preston
42,016 PointsNSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.1.73/TM.php?id=%@",/*self.scannedValue */]]];
is that the line you are talking about? where do you set self.scannedValue at?