我目前正在学习SWIFT,并且我正在尝试了解错误。由于查询,我正在尝试从PHP脚本中获取XML文件,并且我正在使用完整的处理程序将这些查询的结果作为回调。
问题是我对这些技术不满意,它说缺少争论。
这是使用完成处理程序的功能:
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
我已经复制了粘贴您的代码,并且它正在工作,没有任何错误。尝试关闭并在Xcode上。
还必须将completion(output)
移至任务完成,因为它是异步的,没有该输出将始终为空:
(...)
let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print("responseString = (responseString)")
output = responseString as! String
completion(output)
}
task.resume()
(...)
这非常奇怪。我只是再次尝试,当您问的时,我将完成放置在正确的位置,然后重新启动Xcode,对其进行了更新并重新启动了我的计算机,但我仍然有一个错误:呼叫中的参数#2缺少参数。这次,如果我单击错误,它向我提出了一些建议,以添加两个新参数,例如:
func misctest()
{
let dbc : dataBaseCloner = dataBaseCloner()
let output: String
connect({ (output) in
print(output)
}, <#UnsafePointer<sockaddr>!#>, <#socklen_t#>)
print("working!")
}
,但是它不起作用,说我:
cannot convert value of type '(Any)->()' to expected argument type'int32'
这很奇怪。