在Swift 3中获取GCD标签 [英] Get GCD label in Swift 3

查看:128
本文介绍了在Swift 3中获取GCD标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码可以获取当前GCD队列的标签以进行记录,如Swift 2所示:

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 + "] "
}

在Xcode 8将其转换为Swift 3之后,它看起来像这样:

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 + "] "
}

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

However Xcode gives me the following error when I build:

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

Value of tuple type '()' has no member 'label'

在Swift 3中我没有找到任何方法来获取当前队列标签.有人可以帮忙吗?

I haven't found any way to get the current queue label in Swift 3. Can someone help?

谢谢,
David

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)")
}

推荐答案

您可以使用该方法

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

这篇关于在Swift 3中获取GCD标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆