迅速3完成处理程序以返回字符串 [英] swift 3 completion handler to return string
问题描述
我是新手,从函数获取字符串时遇到问题,我正在尝试使用完成处理程序,但是出了点问题,您能帮我吗?
I am new at swift and got problem with getting string from function, I am trying to use completion handler, but something is wrong, could you help me?
将[String:String]添加到func之后,我无法获得响应,我想获取响应并进行打印.错误:无法将类型()的返回表达式转换为返回类型[String:String]
After adding [String : String] to func, I cant get rezult, I want to get response and print it. Error: Cannot convert return expresion of type () to return type [String : String]
请求:
public func login(userName: String, password: String) -> [String : String]{
let loginrequest = JsonRequests.loginRequest(userName: userName, password: password)
return makeWebServiceCall(urlAddress: URL, requestMethod: .post, params: loginrequest, completion: { (JSON : Any) in
print("\(JSON)")
})
}
private func makeWebServiceCall (urlAddress: String, requestMethod: HTTPMethod, params:[String:Any], completion: @escaping (_ JSON : Any) -> ()) {
Alamofire.request(urlAddress, method: requestMethod, parameters: params, encoding: JSONEncoding.default).responseJSON { response in
switch response.result {
case .success:
if let jsonData = response.result.value {
completion(jsonData)
}
case .failure(let error):
if let data = response.data {
let json = String(data: data, encoding: String.Encoding.utf8)
print("Failure Response: \(json)")
}
呼叫功能:
let retur = Json()
let rezultatas = retur.login(userName: "root", password: "admin01")
print(rezultatas)
错误: 在此处输入图片描述
推荐答案
欢迎使用Swift :)
Welcome to Swift :)
您正在将同步和异步代码混合在一起.
You are mixing synchronous and asynchronous code together.
当您呼叫login
时,您希望它立即返回类型为[String : String]
的答案.
When you call login
you expect it to return an answer straight away of type [String : String]
.
但是,在您的login
方法中,您进行了无法立即返回的网络调用...这就是为什么对Alamofire.request
的调用将完成块作为参数的原因.
But in your login
method you then do a network call which can not return straight away...which is why the call to Alamofire.request
takes a completion block as a parameter.
所以...您需要更改您的login
方法,以便:
So...you need to change your login
method so it:
- 不立即返回任何内容(不能这样做...登录要求我们进行网络通话记住)
- 登录成功后,将调用一个完成块.
可以这样做:
public func login(userName: String, password: String, loginCompletion: @escaping ([String : String]) -> ())
这里我们有一个函数,该函数采用类型String
的userName
,类型String
的password
和类型function
的loginCompletion
,该函数再次使用[String : String]
字典作为参数.请注意,该方法不返回任何内容.
Here we have a function that takes a userName
of type String
, a password
of type String
and a loginCompletion
of type function
that again takes a [String : String]
dictionary as a parameter. Notice that the method does not return anything.
现在,您几乎可以像以前一样拨打makeWebServiceCall
了:
Now you can call your makeWebServiceCall
almost as before:
let loginrequest = JsonRequests.loginRequest(userName: userName, password: password)
makeWebServiceCall(urlAddress: URL, requestMethod: .post, params: loginrequest, completion: { (JSON : Any) in
//Now we are ready, the login call has returned some data to you.
//You have an attribute named JSON of type Any, which you need to convert to [String : String], and then you can call loginCompletion with that, like so:
loginCompletion(yourConvertedDictionaryHere)
})
这是完整的新login
方法:
public func login(userName: String, password: String, loginCompletion: @escaping ([String : String]) -> ()) {
let loginrequest = JsonRequests.loginRequest(userName: userName, password: password)
makeWebServiceCall(urlAddress: URL, requestMethod: .post, params: loginrequest, completion: { (JSON : Any) in
//Now we are ready, the login call has returned some data to you.
//You have an attribute named JSON of type Any, which you need to convert to [String : String], and then you can call loginCompletion with that, like so:
loginCompletion(yourConvertedDictionaryHere)
})
}
然后像这样调用login
方法:
retur.login(userName: "root", password: "admin01") { stringDictionary: [String : String] in
//here you have your stringDictionary which you can use as pleased
}
希望对您有帮助.
这篇关于迅速3完成处理程序以返回字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!