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
Adrian Catuna
2,092 PointsiPhone/IOS/Parse - Problems after retrieving data from a one-to-many relationship
This is the code I am using to retrieve my class data from Parse.
PFQuery *query = [PFQuery queryWithClassName:@"UserSettings"]; [query whereKey:@"belongsToUser" equalTo:[PFUser currentUser]]; [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { if (error) { NSLog(@"Error: %@ %@",error, [error userInfo]); } else{ //can't figure out how to extract the data from the objects NSArray that it was stored into above } }];
So when this data is retrieved, it is stored in the objects NSArray. There are 3 pieces of data in that objects array that I need to populate into 3 separate textfields.
I know that data is there because if I NSLog the objects array I can see the data. This is the NSLog code.
NSLog( @"%@", objects);
and it returns this
"<UserSettings:2w12ebzirw:(null)> { \n belongsToUser = \"<PFUser:KttMAy9Sk3>\"; \n companyAddress = \"1234 W. Peoria\"; \n companyCity = Peoria; \n companyName = \"Adrian LLC\";\n}"
So the data is there. I need the companyAddress, companyCity, and companyName. I would like to be able to use the objectForKey option so I can give it the key "companyAddress" and get back "1234 W. Peoria". But NSArray doesn't have the objectForKey option. Even if I try to use objectAtIndex I get an error. This is my objectAtIndex code
NSLog( @"%@", [objects objectAtIndex:1]);
and this is the error
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
What am I doing wrong? How can I retrieve my data and assign it to separate textfields?
Adrian Catuna
2,092 PointsAdrian Catuna
2,092 PointsWas answered here --> https://teamtreehouse.com/forum/using-nsarray-received-from-parsecom