0

I have a mutable array named "eventnameArray". What i want is to add objects. This objects to add come from a Parse query.

This is the mutable array:

eventArray = [[NSMutableArray alloc]init];

This is the Parse query, which works.

PFQuery *query = [PFQuery queryWithClassName:@"Event"];
[query selectKeys:@[@"EventName"]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        // The find succeeded.
        NSLog(@"Successfully retrieved %lu scores.", (unsigned long)objects.count);
        // Do something with the found objects


       [eventnameArray addObject:[objects valueForKey:@"EventName"]];



        NSLog(@"%@", eventnameArray);

    } else {
        // Log details of the failure
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }
}];

I suspect the problem is in the addObject call, which is not being performed correctly, or maybe in the whole PFQuery structure.

To show the array in the tableviewcell, i do this:

cell.label.text = [self.eventnameArray objectAtIndex: [indexPath row]];

EDIT :

I replaced addObject for addObjectsFromArray like this:

[eventnameArray addObjectsFromArray:[objects valueForKey:@"EventName"]];

But i still can't get it to work.

EDIT 2:

I replaced EDIT 1 with :

for (NSObject *object in objects){
            NSString *name = [object valueForKey:@"EventName"];
            [eventnameArray addObject:name];
        }

But the label still doesn't show anything.

EDIT 3 :

The solution was just a matter or reloading data like this:

[self.tableView reloadData]; 
2
  • What does this [objects valueForKey:@"EventName"] return? Commented Mar 13, 2015 at 10:29
  • It returns an Array of the actual String values of the EventName parse column Commented Mar 13, 2015 at 10:33

2 Answers 2

2

I guess what you're looking for is to replace addObject: with addObjectsFromArray: because parse returns an array to you so calling valueForKey:@"EventName" will return an array and you want to add all of those names to your eventArray.

So:

[eventnameArray addObjectsFromArray:[objects valueForKey:@"EventName"]];
Sign up to request clarification or add additional context in comments.

5 Comments

It seems that theres a problem inside the query because if i do a normal addobject with @"hello" for example to the array outside the PFQuery, the label shows "hello", so i don't know what can be happening
Do you mean that the table view isn't updated to display the new items in the array?
thats correct!. I tried Bensge's solution which makes total sense, but i still can't get the label to show anything.
So this is a separate problem to updating the array, you need to call [self.tableView reloadData] after you have updated the array... You should have seen the array log the correct contents with either my code or that from @Bensge
Finally that was the solution !
0

You probably want to add the EventName of each object in the objects array to your eventnameArray, not the "EventName" value of the objects array. Try this instead:

for (NSObject *object in objects){
    NSString *name = [object valueForKey:@"EventName"];
    [eventnameArray addObject:name];
}
//EDIT: Addition for a complete fix. Reload the tabeview afterwards.
[self.tableView reloadData]; 

2 Comments

You contributed a lot to the solution ! Thank you
Marking as an answer is appreciated :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.