Is there a way to print out the current thread id on which the current method is executing on?
(objective-c please)
#include <pthread.h>
...
mach_port_t machTID = pthread_mach_thread_np(pthread_self());
NSLog(@"current thread: %x", machTID);
In Swift
print("Current thread \(NSThread.currentThread())")
NSLog(@"%@", [NSThread currentThread]);
In Swift4
print("\(Thread.current)")
In Swift 5
print("Current thread \(Thread.current)")
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;
}