'String'不符合预期类型'CVarArg'

O-m*_*kar 9 swift vapor

当我正在尝试登录时,我正面临着这个错误.

remote: /tmp/build_f459d376d1bc10ac2e93e52575ac5ea9/Sources/App/main.swift:368:49: error: argument type 'String' does not conform to expected type 'CVarArg'
remote:                     NSLog("FILE NOT AVAILABLE", "TESTNOTI")
remote:                                                 ^~~~~~~~~~
remote:                                                            as! CVarArg
Run Code Online (Sandbox Code Playgroud)

mycode的

if fileManager.fileExists(atPath: (drop.config["servers", "default", "KeyURL"]?.string ?? "default")) {
    NSLog("FILE AVAILABLE", "TESTNOTI")
} else {
    NSLog("FILE NOT AVAILABLE", "TESTNOTI")
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*n R 15

NSLog格式字符串作为第一个参数,后跟一个参数列表,它们替换格式字符串中的占位符(比较字符串格式说明符).

在Apple平台上,您可以String使用以下%@格式打印:

let fileName = "the file"
NSLog("File not found: %@", fileName)
Run Code Online (Sandbox Code Playgroud)

但是,这在Linux平台(例如Vapor)上不起作用.在这里,您必须将Swift字符串转换为C字符串,以便将其作为参数传递给NSLog(并使用%sC字符串的格式):

let fileName = "the file"
fileName.withCString {
    NSLog("File not found: %s", $0)
}
Run Code Online (Sandbox Code Playgroud)