不进行讲解,直接上代码。
1 #import <Foundation/Foundation.h> 2 3 @interface Demo01 : NSObject 4 5 6 -(void) run; 7 -(void) start; 8 9 @end
1 #import "Demo01.h" 2 3 @implementation Demo01 4 5 -(void) run 6 { 7 static int counter = 0; 8 9 while(![[NSThread currentThread]isCancelled]){ 10 [NSThread sleepForTimeInterval:1]; 11 NSLog(@"当前数值:%i",counter++); 12 } 13 14 } 15 16 -(void) start 17 { 18 [NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil]; 19 } 20 21 @end
1 #import <Foundation/Foundation.h> 2 #import "Demo01.h" 3 4 int main(int argc, const char * argv[]) 5 { 6 7 @autoreleasepool { 8 9 // insert code here... 10 NSLog(@"Hello, World!"); 11 12 [[[Demo01 alloc] init]start]; 13 14 while (YES) { 15 16 [[NSRunLoop currentRunLoop]runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; 17 } 18 19 } 20 return 0; 21 }