退出嵌套方法后,为什么我的Bool值更改为false?

时间:2016-12-30 07:08:03

标签: ios swift swift3

我尝试按如下方式执行登录:

 func login()->Bool
    {
            var result:Bool = false;
            var request = URLRequest(url: URL(string: "http://something.com/authenticate")!)
            request.httpMethod = "POST"
            let postString = "email=\(usernameField.text!)&password=\(passwordField.text!)"
            print("email=\(usernameField.text!)&password=\(passwordField.text!)")
            request.httpBody = postString.data(using: .utf8)
            request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
            let task = URLSession.shared.dataTask(with: request) { data, response, error in
                guard let data = data, error == nil else {
                    // check for fundamental networking error
                    print("error=\(error)")
                    result = false;
                    return
                }
                if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
                    // check for http errors
                    print("statusCode should be 200, but is \(httpStatus.statusCode)")
                    print("response = \(response)")
                    print("request = \(request)")
                    result = false
                }
                let responseString = String(data: data, encoding: .utf8)
                result = true;
                print("responseString = \(responseString)")
                self.processResponse(jsonData:data)
            }
            task.resume()
            return result;
    }

我的结果'变量总是被解析为false,即使命中行result = true。

如何在嵌套方法中将其设置为true?

1 个答案:

答案 0 :(得分:1)

您正在使用阻止,并且当您呼叫时,阻止不会同时呼叫"登录"方法。因此,您需要实现接收结果的块。请尝试以下代码:

func login(block:((Bool) -> Void)!)
{
    var result:Bool = false;
    var request = URLRequest(url: URL(string: "http://something.com/authenticate")!)
    request.httpMethod = "POST"
    let postString = "email=\(usernameField.text!)&password=\(passwordField.text!)"
    print("email=\(usernameField.text!)&password=\(passwordField.text!)")
    request.httpBody = postString.data(using: .utf8)
    request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
    let task = URLSession.shared.dataTask(with: request) { data, response, error in
        guard let data = data, error == nil else {
            // check for fundamental networking error
            print("error=\(error)")
            result = false;
            block(result)
            return
        }
        if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
            // check for http errors
            print("statusCode should be 200, but is \(httpStatus.statusCode)")
            print("response = \(response)")
            print("request = \(request)")
            result = false
        }
        if data != nil
        {
            result = true
        }
        let responseString = String(data: data, encoding: .utf8)

        print("responseString = \(responseString)")
        self.processResponse(jsonData:data)
        block(result)
    }
    task.resume()
}