Tuesday, September 24, 2013

object-c block problem

- (NSString *) generate: (NSArray *) data withBlock: (BOOL (^) (NSString *)) aBlock; {
NSMutableString * str = [NSMutableString string];
for (NSString * v in data) {
if (aBlock (v)) {
[Str appendString: v];
}
}
}
Trouble to ask Ha, what is the meaning of this code?

Reply:
^ Is the block block, this function is to let you block incoming rules of conduct into

Such as: NSArray * arry = [[NSArray alloc] initWithObjects: @ "a", @ "b", @ "c", nil];
[Self generate: arry withBlock: ^ BOOL (NSString * a) { return [a isEqualToString: @ "a"] ;/ / here the rules of conduct you can dynamically pass in. < / span>

}];
So that only "a" will be added in str
Reply:
Trouble questioning look, a block with a method, the wording must be:
withBlock: (BOOL (^) (NSString *)) aBlock; semicolon talk block behind the structure?

{
NSMutableString * str = [NSMutableString string];
for (NSString * v in data) {
if (aBlock (v)) {
[Str appendString: v];
}
}
Reply:
Because I saw you called, and no semicolon;
Reply:
That definition semicolon meaningless, you can remove
Like code last semicolon and two semicolons sense, but more a meaningless code
int i = 0;; / / last semicolon meaningless

No comments:

Post a Comment