隐式循环
定义一个Photo类,并带有2个draw方法, 一个带参数、一个不带参数。
Photo.h
#import <Foundation/Foundation.h> @interface Photo : NSObject { @private } -(void) draw; -(void) draw:(NSNumber*) number; @end
Photo.m
#import "Photo.h" @implementation Photo - (id)init { self = [super init]; if (self) { // Initialization code here. } return self; } - (void)dealloc { [super dealloc]; } -(void) draw { [self draw:[NSNumber numberWithInt:(arc4random() % 100)]]; } -(void) draw:(NSNumber *)number { NSLog(@"%@", number); } @end
main.m
#import <Foundation/Foundation.h> #import "Photo.h" int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; //创建Photo数组 NSArray *photos = [NSArray arrayWithObjects:[[Photo alloc] init],[[Photo alloc] init], nil]; //不带参数 [photos makeObjectsPerformSelector:@selector(draw)];//这里不用有:,是因为没有参数 //带参数 [photos makeObjectsPerformSelector:@selector(draw:) withObject:[NSNumber numberWithInt:(arc4random() % 101 +100)]]; [pool drain]; return 0; }
结果:
[Switching to process 4348 thread 0x0] 2011-05-10 10:34:23.036 demo01[4348:903] 85 2011-05-10 10:34:23.039 demo01[4348:903] 33 2011-05-10 10:34:23.040 demo01[4348:903] 153 2011-05-10 10:34:23.040 demo01[4348:903] 153