加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – ‘String’不符合预期类型’CVarArg’

发布时间:2020-12-14 04:56:09 所属栏目:百科 来源:网络整理
导读:当我正在尝试登录时,我正面临着这个错误. 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: /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

mycode的

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平台上,您可以使用%@格式打印字符串:

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读