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 PointsUsing NSArray received from parse.com
I can't figure out how to use an NSArray called 'objects" that I retrieved from my parse database. This is how I am getting the NSArray.
PFQuery *query = [PFQuery queryWithClassName:@"UserSettings"];
[query whereKey:@"belongsToUser" equalTo:[PFUser currentUser]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
How can I access the data in that 'objects' array. If I NSLog the array I can see the data but if I try accessing it using the methods of the array, I get errors.
Is this not the proper way to access my data from parse.
8 Answers
Brett Doffing
4,978 PointsNot sure what you're trying to do with the array that's causing the errors (which may be the problem), but you should be able to work with the array just fine inside the block. If you want to work with the array outside of the block, try creating an array (myArray) just before the block, then while inside the block, set myArray equal to objects.
NSArray *myArray;
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
myArray = [NSArray arrayWithArray:objects];
}
NSLog(@"%@", myArray);
Adrian Catuna
2,092 PointsThanks for your reply Brett.
My goal is to assign 3 pieces of data (companyName, companyAddress, companyCity) from the objects array into 3 textfields in my app. I do want to use the array in the block because I want to make sure there were no errors with the query. This is my code with the block. See my comments.
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{
//I want to populate the text fields here
self.companyName.text = ???
self.companyAddress.text = ???
self.companyCity.text = ???
//I've tried using [objects objectAtIndex:1] but the app crashes with the reply 'index 1 beyond bounds [0 .. 0]'
}
}];
This is what the array data looks like when I NSLog it, NSLog(@"%@",objects)
2014-06-07 17:35:42.892 Bill of Lading[14278:60b] ( "<UserSettings:vm7fPEVwOv:(null)> {\n belongsToUser = \"<PFUser:KttMAy9Sk3>\";\n companyAddress = \"1234 W. Main St\";\n companyCity = Peoria;\n companyName = \"Adrian LLC\";\n}"
This looks a little odd to me and maybe this is why I can't retrieve the data from it as usual. Maybe Im not retrieving my data from parse correctly with my code block above. Is there a different way to get it. I can't find anything else in parses documentation.
Thanks in advance for your help.
Brett Doffing
4,978 PointsIf you're only retrieving one object, it would be at index 0, not 1. If you call [objects lastObject], the app won't crash, even if the array is empty, and if there is only one object, it would grab the object at index 0.
Adrian Catuna
2,092 PointsStill crashes. This time with this message.
2014-06-07 18:08:01.947 Bill of Lading[14328:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[PFObject rangeOfCharacterFromSet:]: unrecognized selector sent to instance 0x10a63a9d0'
Brett Doffing
4,978 PointsIs it crashing in trying to access the data of object at index 0? It has been a while since I've messed with Parse, so I'm not exactly sure what you have to access. Since logging the array works, try logging [objects lastObject]
Adrian Catuna
2,092 PointsYes, its crashing when I try to access the data at index 0 or at lasObject.
If I log it, it doesn't crash. This is what it displays.
2014-06-07 18:22:10.898 Bill of Lading[14374:60b] <UserSettings:vm7fPEVwOv:(null)> { belongsToUser = "<PFUser:KttMAy9Sk3>"; companyAddress = "1234 W. Main St"; companyCity = Peoria; companyName = "Adrian LLC"; }
All the data is there. I need to access that companyAddress value, companyCity value, and companyName value, and assign them to 3 different text fields.
I think the objects array has the whole object (the NSLog data above) in its first index. So its like an array in the first index of an array.
Brett Doffing
4,978 PointsIndeed the data is there as a single object., I simply don't know what that object is, since again, it's been awhile since I've used Parse. If I were to guess, it is a PFObject, since that is what the method seems to be trying to find. Can I ask if you know what UserSettings is? I'm guessing the PFUser data is the same as the current user, or am I wrong in this assumption?
Brett Doffing
4,978 PointsAt this point I would venture to guess that you have been returned a PFObject with a class name of UserSettings, and that the actual data you want is the same as your current user, for which you could just access the data you seem to want directly from the current user. It looks to me that you don't want any data from UserSettings anyway. Let me know if I'm wrong.
Adrian Catuna
2,092 PointsYes, it is a PFObject. Im new to parse (and IOS) and I didn't realize that the data was coming all back as one object and being placed into the first index of my NSArray. Thanks for helping me realize this.
So I'm thinking that I have to assign the first item of my NSArray (lastObject) to a separate array and then I can access the data from it. Or should I create a PFObject and assign it to that?
Brett Doffing
4,978 PointsView previous comment I just posted, and let me know.
Brett Doffing
4,978 PointsI'm going to go out on a limb and say to try:
PFUser *user = [PFUser currentUser];
self.companyName.text = user[@"companyName"];
self.companyAddress.text = user[@"companyAddress"];
self.companyCity.text = user[@"companyCity"];
In which case you wouldn't even have to make the call to retrieve an object.
Adrian Catuna
2,092 PointsBrett I couldn't use this code because the data I need is not in the PFUser object. Its in the UserSettings object which is a separate object that has a relation to the PFUser object, so I still need to call to retrieve the UserSettings object.
However, since you helped me realize that the data that is being returned is actually a PFObject that is stored in the first index of my NSArray I was able to get my data by assigning it to an NSDictionary and then retrieving it from there.
NSDictionary *settings = [objects lastObject];
self.companyName.text = [settings objectForKey:@"companyName"];
self.companyAddress.text = [settings objectForKey:@"companyAddress"];
self.companyCity.text = [settings objectForKey:@"companyCity"];
Thanks for steering me in the right direction.
Brett Doffing
4,978 PointsGlad I could help in working through it.
Adrian Catuna
2,092 PointsBrett, I had to step away from the computer. I will try this as soon as I get back and give you an update. Thanks for the help.
Adrian Catuna
2,092 PointsAdrian Catuna
2,092 PointsWasn't sure which of your replies to mark as Best Answer since it was a combination of them all that led to my resolution, so I just marked this first one. Thanks.