将参数传递到块中

时间:2015-12-23 03:00:39

标签: ios objective-c block

我试图将参数传递给一个块,但我所做的每一个配置都是抛出错误。尝试传入的参数是Venue类型,如下所示。

这是标题中可能不正确的声明

import csv
import collections
with open('in.csv', newline='') as f, open('out.csv', newline='') as result:
    next(f) # skip the input header row
    vals = collections.defaultdict(list) # start a default dict of {item:[],...}
    for row in csv.reader(f): # iterate through input CSV
        # find or create the key e.g. "2.1 53.2", and
        # append the id to its list of values
        vals['{} {}'.format(*row[:2])].append(row[2])
    # create a header row by adding ['id'] to a list of "valx" strings
    # by finding the maximum list by length, finding its length,
    # iterating over a range of that number,
    # and creating the necessary "valx" strings
    csv.writer(result, dialect='excel').writerow(['id'] + ['val{}'.format(num) for num in range(len(max(vals.values(), key=len)))])
    for k,v in sorted(vals.items()): # iterate over the items in the sorted dictionary
        # create an appropriate list by adding the key and the values, and write it
        csv.writer(result, dialect='excel').writerow([k] + list(v))

这是我的实现,我知道这是错误的,因为我无法引用传入的变量,但它唯一不会抛出错误

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

以及如何我称之为

-(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);



    }
}];


}

又错了。基本上我想知道如何以一种允许我传入Venue *类型的变量的方式声明它

1 个答案:

答案 0 :(得分:1)

格式如下:

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

此示例来自http://goshdarnblocksyntax.com/

但是你并没有试图设置" returnType"块,我认为你只想通过一个" Venue *"进入方法,然后在PFQuery找到它的对象时调用块?

在这种情况下,您需要做更多类似的事情:

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

然后在方法内部执行以下操作:

[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);
}];