ios - 在Swift 3中获取GCD标签

标签 ios xcode swift3 grand-central-dispatch xcode8

我有一些代码可以获取当前GCD队列的标签以进行记录,在Swift 2中看起来像这样:

if let queueName
    = String(UTF8String: dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL)) where !queueName.isEmpty {
    detailedMessage += "[" + queueName + "] "
}

Xcode 8将其转换为Swift 3之后,它看起来像这样:
if let queueName
    = String(validatingUTF8: DISPATCH_CURRENT_QUEUE_LABEL.label), !queueName.IsEmpty {
    detailedMessage += "[" + queueName + "] "
}

但是,Xcode在构建时给我以下错误:

元组类型'()'的值没有成员'label'

在Swift 3中,我找不到任何获取当前队列标签的方法。有人可以帮忙吗?

谢谢,
大卫

更新了
这是上下文的功能:
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)")
}

最佳答案

您可以使用该方法

let cName = __dispatch_queue_get_label(nil)
let name = String(cString: cName, encoding: .utf8)

关于ios - 在Swift 3中获取GCD标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40186868/

相关文章:

ios - 如果不在 Xcode 中打开它,如何判断 iOS 项目是否正在使用 ARC?

cocoapods - SwiftyJSON 和 Swift 3 : Cannot convert return expression of type 'Int32?' to return type > 'Int?'

ios - Swift-对NSSSet的内容进行排序

ios - Flutter(在 iOS 上)- 在运行时请求位置权限,没有出现对话框! (定位包)

ios - setlocale 在 iOS 模拟器中有效,但在设备上失败

ios - keyboardWillShow 如何在 Swift iOS 中仅应用于 1 个 textView

ios - 如何使用 Swift iOS 监听 UIReturnKeyType.Next

ios - Google OAuth 2.0 invalid_client 未经授权

ios - 具有存储属性的扩展

ios swift 如何与自定义单元格中的网点交互