NSLog 不可用

NSLog is unavailable

我有以下功能:

func myNSLog(_ givenFormat: String, _ args: CVarArg..., _ function:String = #function) {
        let format = "\(function): \(givenFormat)"
        NSLog(format, args)

这会导致以下错误:

'NSLog' has been explicitly marked unavailable here (Foundation.NSLog)

在文档中明确列为可用。我想念什么?

与C类似,不能直接传递可变参数列表 到另一个功能。您必须创建一个 CVaListPointer(Swift 相当于 va_list) 并将其传递给 NSLogv 变体:

func myNSLog(_ givenFormat: String, _ args: CVarArg..., _ function:String = #function) {
    let format = "\(function): \(givenFormat)"
    withVaList(args) { NSLogv(format, [=10=]) }
}

(Swift 3 个代码。)