NSThread
方法一:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
- ( void )viewDidLoad
{ [ super
viewDidLoad];
NSThread
* thread
= [[ NSThread
alloc]initWithTarget: self
selector: @selector (test:) object:@ "fuck" ];
[ thread
start];
} - ( void )test:( NSString
*)string
{ for
( int i = 0; i < 100; i ++) {
NSLog (@ "%@,%d" ,string,i);
}
} |
方法二:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
- ( void )viewDidLoad
{ [ super
viewDidLoad];
//这种比较快速,不用调用start方法。
[ NSThread
detachNewThreadSelector: @selector (test:) toTarget: self
withObject:@ "you" ];
} - ( void )test:( NSString
*)string
{ for
( int i = 0; i < 100; i ++) {
NSLog (@ "%@,%d" ,string,i);
}
} |
方法三:使用较多
1
2
3
4
5
6
7
8
9
10
11
12
13
|
- ( void )viewDidLoad
{ [ super
viewDidLoad];
//这个方法是从NSObject继承下来的,所有对象都可以通过这个方法快速创建一个线程去执行test中的方法。
[ self
performSelectorInBackground: @selector (test:) withObject:@ "哈哈" ];
} - ( void )test:( NSString
*)string
{ for
( int i = 0; i < 100; i ++) {
NSLog (@ "%@,%d" ,string,i);
}
} |