objective-c 获取当前方法调用的线程 id
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1616572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting thread id of current method call
提问by Alexi Groove
Is there a way to print out the current thread id on which the current method is executing on?
有没有办法打印出当前方法正在执行的当前线程ID?
(objective-c please)
(请目标-c)
回答by nall
NSLog(@"%@", [NSThread currentThread]);
回答by neoneye
#include <pthread.h>
...
mach_port_t machTID = pthread_mach_thread_np(pthread_self());
NSLog(@"current thread: %x", machTID);
回答by dimo hamdy
In Swift 5
在斯威夫特 5
print("Current thread \(Thread.current)")
回答by Glauco Neves
In Swift
在斯威夫特
print("Current thread \(NSThread.currentThread())")
回答by Jason
In Swift4
在Swift4 中
print("\(Thread.current)")
打印(“\(线程。当前)”)
回答by Wiz
you can hack something up like this (this just prints pretty, but you can go ahead and split until you get the number):
你可以像这样修改一些东西(这只是打印得很漂亮,但你可以继续拆分直到你得到数字):
+ (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;
}

