字典不能转换为Void [英] Dictionary is not convertible to Void
问题描述
大家好,我一直在网上搜索没有太多运气,但我正试图绕过Alamofires异步性质。我试图将JSON响应作为字典返回,但Xcode给了我字典不能转换为'Void'
Hi guys I've been searching the net without much luck but I'm trying to get around Alamofires asynchronous nature. I'm trying to return the JSON response as a dictionary but Xcode is giving me "Dictionary is not convertible to 'Void'"
func homePageDetails(userName:String) -> (Dictionary<String,AnyObject>){
let username = userName
let hompePageDetails = Alamofire.request(.GET, "http://example.com/API/Bunch/GetHomePageDetails/\(username)/").responseJSON{(request, response, JSON, error) in
print(JSON)
var test = JSON as Dictionary<String,AnyObject>
return test
}
}
任何帮助都会很大赞赏。
Any help would be greatly appreciated.
推荐答案
您将返回 test:Dictionary< String,AnyObject>
来自闭包,而不是来自 homePageDetails
方法。闭包返回类型是 Void
,这就是你得到这个错误的原因。
You are returning test: Dictionary<String,AnyObject>
from closure and not from homePageDetails
method. Closure return type is Void
, thats why you get this error.
我自己没有使用过Alamofire,但是 Alamofire.request
似乎是非阻塞呼叫。如果是这样你就无法从这个功能返回。你可以使用像完成闭包这样的东西。未经测试的概念:
I haven't used Alamofire myself, but Alamofire.request
seems like non-blocking call. If its so you cannot return from this function. You can use something like completion closure. Non tested concept:
func homePageDetails(userName:String, completion:(Dictionary<String,AnyObject>) -> Void) {
let username = userName
let hompePageDetails = Alamofire.request(.GET, "http://example.com/API/Bunch/GetHomePageDetails/\(username)/").responseJSON{(request, response, JSON, error) in
print(JSON)
var test = JSON as Dictionary<String,AnyObject>
completion(test)
}
}
这篇关于字典不能转换为Void的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!