Objective C - what is the usage of a non-void block? -
i've seen many blocks void homecoming type. it's possible declare non-void blocks. whats usage of this?
block declaration,
-(void)methodwithbock:(nsstring *(^)(nsstring *str))block{ // work block(@"string str"); // phone call }
using method,
[self methodwithbock:^nsstring *(nsstring *str) { nslog(str); // prints phone call homecoming @"ret val"; // <- homecoming value block }];
in above block declaration , purpose of nsstring homecoming type of block? how homecoming value ( "ret val") can used ?
you can utilize non-void blocks same reason you'd utilize non-void function pointer - provide level of indirection when comes code execution.
nsarray
's sortusingcomparator
provides 1 illustration of such use:
nsarray *sorted = [originalarray sortedarrayusingcomparator:(nscomparator)^(id obj1, id obj2){ nsstring *lhs = [obj1 stringattribute]; nsstring *rhs = [obj2 stringattribute]; homecoming [lhs caseinsensitivecompare:rhs]; }];
the comparator block lets encapsulate comparing logic outside sortedarrayusingcomparator
method performs sorting.
objective-c block
No comments:
Post a Comment