NSURLSession 响应字符串完成块 - Swift

NSURLSession Response String completion block - Swift

我想在调用下一个函数 "nextScreen()" (segue) 之前等待 responseString 完成。目前我有一个 if 语句来确保它在继续之前不为 nil,但有时下一个 function/segue 被调用,因为 responseString 仍在下载。

你能帮忙完成一个块吗?我找到了 NSURLSession 的完成块,但这些只是等待初始 HTTP 调用完成,而不是响应字符串。

func getProfiles(){
    func post(completion: (message: String?) -> Void) {
        let request = NSMutableURLRequest(URL: NSURL(string: "http://**.**.**.**/EPG/XML/QueryProfile")!)
        request.HTTPMethod = "POST"
        let postString = "<QueryProfileReq><type>1</type></QueryProfileReq>"
        request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)

        let task: Void = NSURLSession.sharedSession().dataTaskWithRequest(request,
            completionHandler: {(data: NSData!,
                response: NSURLResponse!,
                error: NSError!) in
                if error != nil {
                    println("error=\(error)")
                    let alert = UIAlertView()
                    alert.delegate = self
                    alert.title = "Login Error"
                    alert.message = "\(error)"
                    alert.addButtonWithTitle("OK")
                    alert.show()
                    self.view.endEditing(true)
                    return
                }
                if let responseString = NSString(data: data, encoding: NSUTF8StringEncoding) {
                    if response != nil {
                        println("got profiles")
                        self.nextScreen()
                    }
                    self.dataVar = data // UPDATES VARIABLE TO SEND
                }
        }).resume()

    }
}

dataTaskWithRequest 的便捷方法本质上是returns数据或错误,通常带有一些响应头类型信息。如果您有错误,那么您将没有数据(对此有 99% 的把握)。我已重新格式化您的方法以提供帮助。 NSString Init Convenience 方法是同步的,所以不太确定等待完成而不是 http 调用是什么意思?

func getStringFromRequest(completionHandler:(success:Bool, data: NSData?) -> Void) {

    let request = NSMutableURLRequest(URL: NSURL(string: "http://##.##.##.##/EPG/XML/QueryProfile")!)
    request.HTTPMethod = "POST"
    let postString = "<QueryProfileReq><type>1</type></QueryProfileReq>"
    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)

    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in

        if let unwrappedError = error {
            print("error=\(unwrappedError)")
        }
        else {
            if let unwrappedData = data {
                completionHandler(success: true, data: unwrappedData)
                return
            }
        }

        completionHandler(success: false, data: nil)
    }

    task?.resume()
}

func performPost() {

    getStringFromRequest { (success, data) -> Void in

        if (success) {

            if let unwrappedData = data {

                self.dataVar = unwrappedData

                if let responseString = NSString(data: unwrappedData, encoding: NSUTF8StringEncoding) {
                    self.nextScreen()
                }
            }
        }
        else {
            print("Failed")
        }
    }
}