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

iOS

Add Parse query array to UIPickerView

I have a query that returns an array and I would like to set the picker view title values to the values in this array. I am able to do this with an array declared in the viewDidLoad method but not an array created from a Parse.com query. Thanks

Here is my query:

PFQuery *query = [PFQuery queryWithClassName:@"Post"];
    [query selectKeys:@[@"title"]];
    NSArray *array = [query findObjects];

    NSLog(@"%@",array);

What Does NSLog prints ?

The NSLog prints

2014-08-28 11:48:52.155 AppName[7723:60b] Warning: A long-running operation is being executed on the main thread. 
 Break on warnBlockingOperationOnMainThread() to debug.
2014-08-28 11:48:53.062 AppName[7723:60b] (
    "<Post:K4d3o5GgzA:(null)> {\n    title = test2;\n}",
    "<Post:gs0HzzCIly:(null)> {\n    title = rhhe;\n}",
    "<Post:ETGucmCO7b:(null)> {\n    title = aaa;\n}",
    "<Post:PfOOzjLiFj:(null)> {\n    title = aaa;\n}",
    "<Post:NbP7VNQRh9:(null)> {\n    title = aaa;\n}",
    "<Post:reb3PmgONC:(null)> {\n    title = abb;\n}",
    "<Post:ZqBiNvX1dL:(null)> {\n    title = new;\n}",
    "<Post:s5qIHY33LH:(null)> {\n    title = test;\n}",
    "<Post:G6apcaFv6F:(null)> {\n    title = testing;\n}",
    "<Post:gCEXvaIear:(null)> {\n    title = tester;\n}"
)

1 Answer

Hey tom,

funny because i wrote similar code a couple hours ago, here's what i did.

//i have a mutable array called users to store all the returned objects

//search method where i get the data
PFQuery *query = [PFUser query];
[query whereKey:@"username" containsString:findUserField.text];
[query whereKeyExists:@"profilePicture"];

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            if (error) {
                UIAlertView *errorFindingUsers = [[UIAlertView alloc]initWithTitle:@"Oh no"
                                                                           message:@"An error occured while fetching the users."
                                                                          delegate:nil
                                                                 cancelButtonTitle:nil
                                                                 otherButtonTitles:@"OK", nil];
                [errorFindingUsers show];
                [hud hide:YES];
            }else{
                [self.users addObjectsFromArray:objects];
                NSLog(@"query successful and the first user is %@", objects[0]);
                [self.collectionView reloadData];
                [hud hide:YES];
            }
        }];

//then set my cells
UILabel *userNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(75, 10, 100, 50)];
userNameLabel.text = (NSString*)self.users[indexPath.row][@"username"];
userNameLabel.backgroundColor = [UIColor clearColor];
userNameLabel.font = [UIFont fontWithName:@"Arial-BoldItalicMT" size:20];
[cell addSubview:userNameLabel];

anyways key thing here is that i do

userNameLabel.text = (NSString*)self.users[indexPath.row][@"username"];

so in you case you do the same for your array

NSMutableArray *someArrayOfTitle = [NSMutableArray new];

for(int i = 0 ; i < array.count ; i++){
   NSString *someString =  (NSString*)array[i][@"title"];
   [someArrayOfTitle addObject someString];
}

anyways this should work,

I'm quite tired to I'll review what i wrote tomorrow but in the mean time hope the helps

best, Kai