1

This seems a little strange to me that when my block does not returns anything, I can capture it in a variable before passing it to a consumer metod. But as soon I add a return value to the block typedef, I start getting waring

Scenario 1: Block does not returns anything //declaration

typedef void (^MYConfigureBlock)(MYFeedCell *cell, NSIndexPath *indexPath);

//usage

  MYConfigureBlock block  = ^(MYFeedCell *cell, NSIndexPath *indexPath){
    [cell setActionDelegate:self];
    return nil;
};

MYFeedSource *fds = [[MYFeedSource alloc]initWithTableView:self.tableView
                                                    configurationBlock:block];
[fds setErrorMessage:@"No feeds yet. Is everyone even alive?"];
self.feedDataSource = fds;

Every thing works perfect in the above piece of code unless I go ahead and do:

Problem here

typedef MYFeedCell* (^MYConfigureBlock)(MYFeedCell *cell, NSIndexPath *indexPath);

Now how can I re-write the following statements so that there is no error. And why does this not works as usual with a return type?

MYConfigureBlock block  = ^(MYFeedCell *cell, NSIndexPath *indexPath){
    [cell setActionDelegate:self];
    return nil;
};

The error is

Sending 'MYFeedCell *' to parameter of incompatible type 'MYConfigureBlock' (aka 'MYFeedCell *(^)(MYFeedCell *__strong, NSIndexPath *__strong)')

The code that uses the block

- (UITableViewCell*)tableView:(UITableView*)aTableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {

            self.cellConfigureBlock(nil, indexPath);
}
geekay
  • 1,655
  • 22
  • 31
  • The first parameter is different in the declaration and use; (`UITableViewCell *cell` and `MYFeedCell *cell`). – trojanfoe Jun 26 '14 at 08:09
  • fixed it. i typed the question so please ignore the parameters and variable names. thanks though – geekay Jun 26 '14 at 08:10
  • So it looks like the error is in the code that calls `block`. Post that. – trojanfoe Jun 26 '14 at 08:14
  • do you want to see the declaration of the init method? `-(instancetype)initWithTableView:(UITableView*)aTableView configurationBlock:(MYConfigureBlock)block` – geekay Jun 26 '14 at 08:16
  • No, I'd like to see the code that *uses* `block`. – trojanfoe Jun 26 '14 at 08:16
  • `- (UITableViewCell*)tableView:(UITableView*)aTableView cellForRowAtIndexPath:(NSIndexPath*)indexPath { self.cellConfigureBlock(nil, indexPath);} ` ignore the nil in the argument. – geekay Jun 26 '14 at 08:18

2 Answers2

1

You have to include the block's return type in the block signature because the compiler cannot infer the block's return type if you return nil. Try this:

MYConfigureBlock block = ^ MyFeedCell* (MYFeedCell *cell, NSIndexPath *indexPath){
    [cell setActionDelegate:self];
    return nil;
};

Note that your original code does not raise a compile error if you return a MyFeedCell object because the compiler can infer the return type:

MYConfigureBlock block = ^(MYFeedCell *cell, NSIndexPath *indexPath){
    [cell setActionDelegate:self];
    return cell;
};
Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
-1
    /** PROBLEM is the nil return*/
    //  return nil;

    //SHOULD return a valid type,which is expected.----Here Cell object should return.
    return cell;