2017-03-08 69 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) 
} 
相關問題