can anyone tell flow of execution of blocks in objective c? -


hi iam working blocks in objective-c. have learn syntax, , how write blocks. didn't understand execution flow. googled flow of execution, can't find.

i used following code :

@interface nsarray (extended) - (nsarray *)each:(void (^)(id))block; @end  @implementation nsarray (extended) - (nsarray *)each:(void (^)(id object))block { for(id mobject in self)     block(mobject); return self; } @end  int main (int argc, const char * argv[]) { @autoreleasepool {    nsarray *array = [nsarray arraywithobjects:@"number one", @"number two", nil];   [array each:^(id object) {      nslog(@"obj: %@", object);   }];  } return 0; } 

can explain flow of execution of blocks in objective-c?

not sure how want explain thing. here attempt:

block piece of code. when create block using ^{}, code can stored in variable. in case (and in of cases) immediatelly passed method argument, other value. example block declares 1 argument, value must specified when code executed. variable object_in will contain object , can work in block of code.

[array each:^(id object_in) {     nslog(@"object: %@", object_in); }]; 

inside of method, code stored in argument variable, named block in case. want execute code. appending parentesis the list of real values block arguments. object_out contains real value , passed object_in.

- (nsarray *)each:(void (^)(id))block {     (id object_out in self) {         block(object_out); // executes code of block given argument     }     return self; } 

now nslog statement executed every object in array.

it similar function pointer. if know it, may easier, if not, don't bother :)


so can pass values block arguments, there second way values block. capturing local variables, not used in example. if or else wanted me explain it, feel free comment.


edit: here light explanation captured variables. every piece of code enclosed in {} scope , block scope. scopes nested , local variables created in them valid until control flow leaves scope. child scopes can use variables parent scope.

example code below iterates 2 arrays, 1 for-in loop , second -each: method question. block passed argument different – uses 1 variable parent scope (in fact it's grandparent scope). why block different? method -each: executed multiple times, every time string containing different value. in words, 3 instances of block created each its' own string value.

nsarray *strings = @[ @"a", @"b", @"c" ]; nsarray *numbers = @[ @1, @2, @3 ];  nsstring *string = nil; // doing more obvious (string in strings) {     // string simple local variable     [numbers each:^(id number) {         // number argument         nslog(@"argument: %@, captured: %@", number, string);     }]; } 

output:

argument: 1, captured: argument: 2, captured: argument: 3, captured: argument: 1, captured: b argument: 2, captured: b argument: 3, captured: b argument: 1, captured: c argument: 2, captured: c argument: 3, captured: c 

so have code, common 3 instances , variable (can be) different each of them. if reminds of class / object relationship, right.

argument different each invocation of block (shown in previous code examples) , captured variables different each instance. new instance created every time line block definition (^{) executed.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -