问题
I've got some code that gets the label of the current GCD queue for logging purposes that looks like this in Swift 2:
if let queueName
= String(UTF8String: dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL)) where !queueName.isEmpty {
detailedMessage += "[" + queueName + "] "
}
After Xcode 8 converted this to Swift 3, it looks like this:
if let queueName
= String(validatingUTF8: DISPATCH_CURRENT_QUEUE_LABEL.label), !queueName.IsEmpty {
detailedMessage += "[" + queueName + "] "
}
However Xcode gives me the following error when I build:
Value of tuple type '()' has no member 'label'
I haven't found any way to get the current queue label in Swift 3. Can someone help?
Thanks,
David
UPDATED Here's the function for context:
public func log(_ message: String,
tag: String,
level: Logger.LogLevel,
userInfo: [String : String]?,
functionName: StaticString,
fileName: StaticString,
lineNumber: Int) {
var detailedMessage = ""
let formattedDate = self._dateFormatter.string(from: Date())
detailedMessage += "\(formattedDate) "
detailedMessage += "[\(level.description)] "
if Thread.isMainThread {
detailedMessage += "[main] "
} else {
if let threadName = Thread.current.name , !threadName.isEmpty {
detailedMessage += "[" + threadName + "] "
} else if let queueName
= String(validatingUTF8: DISPATCH_CURRENT_QUEUE_LABEL.label) , !queueName.isEmpty {
detailedMessage += "[" + queueName + "] "
} else {
detailedMessage += "[" + String(format:"%p", Thread.current) + "] "
}
}
let lastPathComponent = NSString(stringLiteral: fileName).lastPathComponent
detailedMessage += "[" + lastPathComponent + ":" + String(lineNumber) + "] "
detailedMessage += "\(functionName) "
let fullMessage = self.messageWithTag(tag, message: message)
detailedMessage += "> \(fullMessage)"
NSLog("\(fullMessage)")
}
回答1:
You can use that method
let cName = __dispatch_queue_get_label(nil)
let name = String(cString: cName, encoding: .utf8)
来源:https://stackoverflow.com/questions/40186868/get-gcd-label-in-swift-3