sendAsynchronousRequest

sendAsynchronousRequest

var arrayData: [String] = []

let bodyData = "parameter=test"

let URL: NSURL = NSURL(string: "Link to php file")
let request:NSMutableURLRequest = NSMutableURLRequest(URL:URL)
request.HTTPMethod = "POST"
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding);
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue())
{
    (response, data, error) in
        var output = NSString(data: data, encoding: NSUTF8StringEncoding)

        self.arrayData = self.JSONParseArray(output)

        println(self.arrayData) //Print the result
}

println(self.arrayData) //Print nothing

新值似乎只在sendAsynchronousRequest中可用
是否有方法使新值可以从sendasyncronousrequest中访问?

最佳答案

sendAsynchronousRequest顾名思义是异步的。因此,您的最终println在请求完成之前运行。在完成处理程序运行之后,新数据将可用于所有读卡器(在此处理程序内部或外部)。

关于swift - 新值仅在sendAsynchronousRequest中可用-Swift,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25532097/

10-10 20:50