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 trialTom Coomer
1,331 PointsDisplay username of query results Parse.com
I have a query and would like to display the username of the results in a label. I would then like to move to the next one on the click of a button.
This is what I currently have:
-(IBAction)search {
PFQuery * query = [PFUser query];
[query whereKey:@"company" equalTo:@"Smudge Inc"];
[query orderByAscending:@"createdAt"];
NSArray *results = [query findObjects];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
NSLog(@"%@",results);
NSLog(@"count = %d", [results count]);
searchRestuls = [results count];
currentResult = 1;
totalResults.text = [NSString stringWithFormat:@"Viewing %d of %d results.",currentResult,searchRestuls];
}];
}
-(IBAction)next {
currentResult = currentResult + 1;
//Display next username from query results
}
Thanks
1 Answer
Stone Preston
42,016 Pointsyou would need to store the array of objects found by the query in a property and also store the current index of that array in a property:
so add an array property (in this example its named resultsArray and an NSInteger (in this case its named currentIndex) property to your class and edit your query code:
havent tested any of this. so it might not work.
-(IBAction)search {
PFQuery * query = [PFUser query];
[query whereKey:@"company" equalTo:@"Smudge Inc"];
[query orderByAscending:@"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
self.resultsArray = objects;
//initialize the currentIndex 0 when the results are found
self.currentIndex = 0;
totalResults.text = [self.resultsArray objectAtIndex:self.currentIndex].username;
}
}];
}
and then in your next action increment the index property and update the text label
-(IBAction)next {
self.currentIndex = self.currentIndex + 1;
if (self.currentIndex > [self.resultsArray count] -1) {
//end of the array, do nothing.
} else {
self.totalResults.text = [self.resultsArray objectAtIndex:self.currentIndex].username;
}
}
Tom Coomer
1,331 PointsTom Coomer
1,331 PointsGreat. Thank you. I will try it out and let you know the results.
Tom Coomer
1,331 PointsTom Coomer
1,331 PointsI have been shown an error message on this line. It says: Property 'username' not found on object of type 'id'.
totalResults.text = [self->resultsArray objectAtIndex:self->currentIndex].username;
Stone Preston
42,016 PointsStone Preston
42,016 Pointshmm, you may have to cast the object in the array to a PFUser object:
totalResults.text = (PFUser *) [self.resultsArray objectAtIndex:self.currentIndex].username;
Tom Coomer
1,331 PointsTom Coomer
1,331 PointsIt still shows the same error.