-
Notifications
You must be signed in to change notification settings - Fork 2
Block
zjy edited this page Jul 13, 2016
·
1 revision
#Block
##What is a block?
A block of code (i.e. a sequence of statements inside {}).
##What does it look like?
Here’s an example of calling a method that takes a block as an argument.
NSString *stopKey = [@“Enough” uppercaseString];
__block BOOL stoppedEarly = NO;
double stopValue = 53.5;
[aDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
NSLog(@“value for key %@ is %@”, key, value);
if ([stopKey isEqualToString:key] || ([value doubleValue] == stopValue)) {
 *stop = YES;
stoppedEarly = YES; // this is legal now
}
}];
if (stoppedEarly) NSLog(@“I stopped logging dictionary values early!”);
stoppedEarly will copy to heap and set in block,then set in stack agagin until block finished
stopKey will automatically have a strong pointer to it until the block goes out of scope This is obviously necessary for the block to function properly