2006-03-21 21:57:31 Quick foreach w/ nsnumbers
NOTE: This page was written in the pre-Objective 2.0 days, when I was working on my own set of changes to Objective-C. My project has since been abandoned in favor of solutions made available both by Objective-C 2.0 and by F-Script.


A quick foreach / nsnumber example. The code:
( objc )
  1  NSArray *a = @( @4, @8, @3.14 );
2 foreach( item in a )
3 print "%s\n", [[item description] cString];

is processed into the objective-c equivalent:
( objc )
  1  #import <Foundation/Foundation.h>
2
3 int main(int argc, const char *argv[]) {
4 NSAutoreleasePool *pool = [NSAutoreleasePool new];
5 NSEnumerator *foreachEnumerator;
6 NSArray *a = [NSArray arrayWithObjects:
7 [NSNumber numberWithInt:4],
8 [NSNumber numberWithInt:8],
9 [NSNumber numberWithFloat:3.14] , nil];
10 foreachEnumerator = [a objectEnumerator];
11 id item;
12 while( item = [foreachEnumerator nextObject])
13 printf( "%s\n", [[item description] cString] );
14 [pool release];
15 return 0;
16 }

To produce the expected output:

  1  4
2 8
3 3.14
Leave a comment