2017-03-08 237 views
4

当我尝试登录时,我正面临着此错误。'字符串'不符合预期类型'CVarArg'

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

份额编写一些代码行。 –

+1

“as!”在哪里?错误信息中的CVarArg'来自哪里?它不在代码中。除此之外,在没有任何占位符('%@')的'NSLog'中使用两个参数是无意义的 – vadian

回答

8

NSLog需要作为第一个参数一个格式字符串,其通过的参数,这些取代在格式字符串中的占位符 列表随后 (比较String Format Specifiers)。

在苹果平台上,可以使用%@格式打印String

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

然而,这并不能在Linux平台上运行(如蒸汽)。 在这里,你必须将斯威夫特字符串转换为C字符串,以通过 它作为一个参数的NSLog(和使用C字符串的%s格式):

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