所以我的视图控制器中有以下代码可以完美运行。我发出 json 请求,发出服务器调用,我得到响应没有问题。点击按钮调用我的函数:
@IBAction func buttonClick(sender: AnyObject) {
var postsEndpoint = "http://test.com/test"
let test = ["Blah":"test"]
request(.POST, postsEndpoint, parameters: test as! [String : AnyObject] , encoding: .JSON)
.responseJSON { (request, response, data, error) in
if let anError = error
{
// got an error in getting the data, need to handle it
println("error calling POST on /posts")
println(error)
}
else if let data: AnyObject = data
{
let post = JSON(data)
// now we have the results, let's just print them though a tableview would definitely be better UI:
println("The post is: " + post.description)
}
}
}
所以现在我想创建一个可以用来调用我所有服务的类。所以我在这个类中创建了一个函数,我从我的视图控制器调用 let invoke = WebServiceCall() let jsonrequest = invoke.InvokeService()
所以我的班级和职能是
class WebServiceCall{
var post:JSON = nil
func InvokeService(type : String) ->JSON{
var postsEndpoint = "http://test.com/test"
let test = ["Blah":"test"]
request(.POST, postsEndpoint, parameters: test as! [String : AnyObject] , encoding: .JSON)
.responseJSON { (request, response, data, error) in
if let anError = error
{
// got an error in getting the data, need to handle it
println("error calling POST on /posts")
println(error)
}
else if let data: AnyObject = data
{
self.post = JSON(data)
// now we have the results, let's just print them though a tableview would definitely be better UI:
println("The post is: " + post.description)
}
}
return self.post
}
我认为我的问题是调用了该函数,但是因为没有完成处理程序来等待我从未得到返回数据的响应。我用谷歌搜索了完成处理程序,但我很困惑。任何帮助都会很棒