NOTE: block is referenced with copy
typedef int (^myblock) (int, int);
void Play ()
{
NSLog (@ "Play mp3");
}
int main (int argc, const char * argv[])
{
@autoreleasepool {
Myblock Sumblock = ^ (int a, int b) {
return a + B;
};
Myblock Minusblock = ^ (int a, int b) {
return a-B;
};
int r = Minusblock (10, 2);
NSLog (@ "%d", r);
}
return 0;
}
——————————————————————————————
Memory leak problem: The block inside refers to self, and self strong pointer to block. So that self cannot be destroyed.
Workaround:
__weak typeof (self) SELFVC = self;
Define a self alias and use the Ruo pointer to reference the
————————————————————————————————
Note: When a block is defined, it is built in block
A copy (copy) of the contents of the current local variable is subsequently modified to the value of the variable, focusing on
Does not affect the values in the block
If you need to keep local variables in the block
If you want to modify a local variable outside the block, you need to use the __block keyword __block BOOL flag = NO;
————————————————
Simple Block & memory leak issues with IOS