iOS block备忘录03 发表于 Jun 16 2016 | 分类于 iOS | Block的常见几个用途1.简化枚举 1234NSArray *array = ... [array enumerateObjectsUsingBlock:^ (id obj, NSUInteger idx, BOOL *stop) { NSLog(@"Object at index %lu is %@", idx, obj); }]; 2.简化并发任务 使用于OperationQueue 1234567891011NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{ ...}];// schedule task on main queue:NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];[mainQueue addOperation:operation];// schedule task on background queue:NSOperationQueue *queue = [[NSOperationQueue alloc] init];[queue addOperation:operation]; 使用于GCD 12345dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);dispatch_async(queue, ^{ NSLog(@"Block for asynchronous execution");}); 坚持原创技术分享,您的支持将鼓励我继续创作! 赏 微信打赏 支付宝打赏