You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.2 KiB
62 lines
1.2 KiB
9 years ago
|
/*
|
||
|
* Copyright (c) 2015 - present Facebook, Inc.
|
||
|
* All rights reserved.
|
||
|
*
|
||
|
* This source code is licensed under the BSD style license found in the
|
||
|
* LICENSE file in the root directory of this source tree. An additional grant
|
||
|
* of patent rights can be found in the PATENTS file in the same directory.
|
||
|
*/
|
||
10 years ago
|
|
||
|
#import <Foundation/Foundation.h>
|
||
|
|
||
|
@interface MyBlock : NSObject
|
||
|
|
||
|
@end
|
||
|
|
||
|
@implementation MyBlock
|
||
|
|
||
9 years ago
|
- (void)array {
|
||
|
|
||
|
NSArray* a = [[NSArray alloc] init];
|
||
|
[a enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL* stop) {
|
||
|
int ShouldStop;
|
||
|
/*
|
||
|
... do something here...
|
||
|
*/
|
||
|
if (ShouldStop) {
|
||
|
*stop = YES;
|
||
|
};
|
||
|
}];
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
- (void)array_trans {
|
||
10 years ago
|
|
||
9 years ago
|
NSArray* a = [[NSArray alloc] init];
|
||
10 years ago
|
|
||
9 years ago
|
NSArray* objects = a;
|
||
10 years ago
|
|
||
9 years ago
|
// The call to the iterator in the above function is translated as:
|
||
|
void (^enumerateObjectsUsingBlock)(id, NSUInteger, BOOL*) =
|
||
|
^(id object, NSUInteger idx, BOOL* stop) {
|
||
9 years ago
|
|
||
10 years ago
|
int ShouldStop;
|
||
9 years ago
|
|
||
10 years ago
|
if (ShouldStop) {
|
||
9 years ago
|
*stop = YES;
|
||
10 years ago
|
};
|
||
9 years ago
|
};
|
||
|
BOOL* stop = malloc(sizeof(BOOL));
|
||
|
*stop = NO;
|
||
|
|
||
|
for (NSUInteger idx = 0; idx < objects.count; idx++) {
|
||
|
|
||
|
id object = objects[idx];
|
||
|
enumerateObjectsUsingBlock(object, idx, stop);
|
||
|
if (*stop == YES)
|
||
|
break;
|
||
|
}
|
||
|
free(stop);
|
||
10 years ago
|
}
|
||
|
|
||
|
@end
|