ian
ian

Reputation: 1002

Passing a parameter into a block

I'm trying to pass a parameter into a block but every configuration i do is throwing errors. The parameter im trying to pass in is of type Venue as you can see below.

Here is my perhaps incorrect declaration in the header

-(void)update:(Venue* (^)(NSArray *myScenes, NSError *error))block;  

Here is my implementation which i know is wrong because i cant reference the passed in variable but its the only thing not throwing an error

-(void)update:(Venue* (^)(NSArray *myScenes, NSError *error))block{

//download scenes
PFQuery *query = [PFQuery queryWithClassName:@"Scenes"];
[query orderByDescending:@"createdAt"];
[query whereKey:@"venueId" equalTo:venue.objectId];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (error) {
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }
    else {
        // We found messages!
        scenes = objects;
        //[PFObject pinAllInBackground:objects];

        NSLog(@"Retrieved %lu messages", (unsigned long)[scenes count]);
        NSLog(@"Venues = %@", scenes);



    }
}];


}

and heres how im calling it

[_venues[0] update:^Venue *(NSArray *myScenes, NSError *error) {
    if (error) {
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }
    else {

        NSLog(@"myObjects are: %@", myScenes);
        _venues[0].scenes = myScenes;


    }
}];

wrong again. Basically i want to know how to declare this in a way that lets me pass in a variable of type Venue*

Upvotes: 0

Views: 105

Answers (1)

siburb
siburb

Reputation: 5017

The format is as follows:

- (void)someMethodThatTakesABlock:(returnType (^)(parameterTypes))blockName;

This example is from http://goshdarnblocksyntax.com/

But you're not trying to set the "returnType" of the block, I think that you just want to pass a "Venue*" into the method, and then call the block when your PFQuery finds it's objects?

In which case, you'd need to do something more like this:

- (void)updateVenue:(Venue*)venue completion:(void(^)(NSArray *myScenes, NSError *error))completion{

And then inside the method do the following:

[query findObjectsInBackgroundWithBlock:^(NSArray *scenes, NSError *error) {
if (error) {
    NSLog(@"Error: %@ %@", error, [error userInfo]);
}
else {
    // We found messages!
    //[PFObject pinAllInBackground:scenes];

    NSLog(@"Retrieved %lu messages", (unsigned long)[scenes count]);
    NSLog(@"Venues = %@", scenes);
}
if (completion) completion(scenes, error);
}];

Upvotes: 1

Related Questions