获取当前方法调用的线程ID

有没有办法打印当前的方法正在执行的当前线程ID?

(请客观)

NSLog(@"%@", [NSThread currentThread]); 
 #include <pthread.h> ... mach_port_t machTID = pthread_mach_thread_np(pthread_self()); NSLog(@"current thread: %x", machTID); 

在Swift3中

 print("Current thread \(Thread.current)") 

在Swift中

 print("Current thread \(NSThread.currentThread())") 

你可以像这样(这只是打印漂亮,但你可以继续前进,拆分,直到你得到的号码):

 + (NSString *)getPrettyCurrentThreadDescription { NSString *raw = [NSString stringWithFormat:@"%@", [NSThread currentThread]]; NSArray *firstSplit = [raw componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"{"]]; if ([firstSplit count] > 1) { NSArray *secondSplit = [firstSplit[1] componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"}"]]; if ([secondSplit count] > 0) { NSString *numberAndName = secondSplit[0]; return numberAndName; } } return raw; }