1/* 2 * Copyright (c) 2010 Apple Inc. All rights reserved. 3 * 4 * @APPLE_LLVM_LICENSE_HEADER@ 5 */ 6 7#import <Foundation/Foundation.h> 8 9#ifndef __BLOCKS__ 10#error compiler does not support blocks. 11#endif 12 13#if !NS_BLOCKS_AVAILABLE 14#error Blocks don't appear to be available, according to the Foundation. 15#endif 16 17NSInteger sortStuff(id a, id b, void *inReverse) { 18 int reverse = (int) inReverse; // oops 19 int result = [(NSString *)a compare: b]; 20 return reverse ? -result : result; 21} 22 23int main (int argc, const char * argv[]) { 24 NSArray *stuff = [NSArray arrayWithObjects: @"SQUARED OFF", @"EIGHT CORNERS", @"90-DEGREE ANGLES", @"FLAT TOP", @"STARES STRAIGHT AHEAD", @"STOCK PARTS", nil]; 25 int inReverse = 1; 26 27 NSLog(@"reverse func: %@", [stuff sortedArrayUsingFunction:sortStuff context: &inReverse]); 28 NSLog(@"reverse block: %@", [stuff sortedArrayUsingComparator: ^(id a, id b) { 29 int result = [a compare: b]; 30 return inReverse ? -result : result; 31 }]); 32 33 inReverse = 0; 34 35 NSLog(@"forward func: %@", [stuff sortedArrayUsingFunction:sortStuff context: &inReverse]); 36 NSLog(@"forward block: %@", [stuff sortedArrayUsingComparator: ^(id a, id b) { 37 int result = [a compare: b]; 38 return inReverse ? -result : result; 39 }]); 40 41 return 0; 42} 43