我目前正在學習swift,並且在嘗試瞭解現在幾個小時以來的錯誤。我試圖從一個PHP腳本得到一個XML文件感謝查詢,我使用完成處理程序來獲取這些查詢的結果作爲回調。swift的完成處理程序
問題是我不熟悉這些技術,它說參數丟失。
下面是一個使用完成處理的功能:
func connect(completion: (String) ->())
{
let password : String = "psw"
let login : String = "log"
let postString : String = "login=\(login)&password=\(password)"
let urlString = "http://www.mydomain.fr/script.php"
var output : String = ""
let request = NSMutableURLRequest(url: NSURL(string: urlString)! as URL)
request.httpMethod = "POST"
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request as URLRequest){
data, response, error in
if error != nil {
print("error=\(error)")
return
}
let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print("responseString = \(responseString)")
output = responseString as! String
}
task.resume()
completion(output)
}
而這裏的一段代碼,我調用這個函數:
func misctest()
{
let dbc : dataBaseCloner = dataBaseCloner()
let output: String
connect(completion : { (output) in
print(output)
})
}
這塊代碼顯示的這個錯誤有關呼叫連接:
>>>Missing argument for parameter #2 in call
我做錯了什麼?爲什麼它需要兩個參數?
非常感謝您的幫助。
Elbattore
這不會修復你的錯誤,但你必須將「完成(輸出)」移動到任務完成狀態,因爲它是異步的,並且沒有輸出將始終爲空 –