gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 21:51:01 27 4
gpt4 key购买 nike

当我尝试使用 NSLog 进行记录时,我遇到了这个错误:

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

这是我的代码:

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

为什么会发生这种情况,我该如何解决?

最佳答案

NSLog 将一个格式字符串作为第一个参数, 紧随其后由参数列表代替占位符在格式字符串中(比较 String Format Specifiers )。

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

let fileName = "the file"
NSLog("File not found: %@", fileName)

但是,这在 Linux 平台(例如 Vapor)上不起作用。这里你必须将 Swift 字符串转换为 C 字符串才能通过它作为 NSLog 的参数(并且对 C 字符串使用 %s 格式):

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

关于swift - 'String' 不符合预期类型 'CVarArg',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42670593/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com