ios - How to store a block definition that returns something, in a local variable -
this seems little strange me when block
not returns anything, can capture in variable before passing consumer metod. add return value block typedef
, start getting waring
scenario 1: block not returns //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. alive?"]; self.feeddatasource = fds;
every thing works perfect in above piece of code unless go ahead , do:
problem here
typedef myfeedcell* (^myconfigureblock)(myfeedcell *cell, nsindexpath *indexpath);
now how can re-write following statements there no error. , why not works usual return type?
myconfigureblock block = ^(myfeedcell *cell, nsindexpath *indexpath){ [cell setactiondelegate:self]; return nil; };
the error is
sending 'myfeedcell *' parameter of incompatible type 'myconfigureblock' (aka 'myfeedcell *(^)(myfeedcell *__strong, nsindexpath *__strong)')
the code uses block
- (uitableviewcell*)tableview:(uitableview*)atableview cellforrowatindexpath:(nsindexpath*)indexpath { self.cellconfigureblock(nil, indexpath); }
you have include block's return type in block signature because compiler cannot infer block's return type if return nil
. try this:
myconfigureblock block = ^ myfeedcell* (myfeedcell *cell, nsindexpath *indexpath){ [cell setactiondelegate:self]; return nil; };
note original code not raise compile error if return myfeedcell
object because compiler can infer return type:
myconfigureblock block = ^(myfeedcell *cell, nsindexpath *indexpath){ [cell setactiondelegate:self]; return cell; };
Comments
Post a Comment