函数返回初始值

时间:2016-09-19 21:34:37

标签: ios swift swift2 swift3

当我调用此函数时,函数返回值,然后执行所有其他操作。我怎样才能使函数返回其他东西的价值结束。

    func register(parameters : [String : String]) -> Int {

    let headers = [
        "content-type": "application/json",
        "cache-control": "no-cache",
    ]
    var statuscode = 0
    var postD = NSData();
    do {
        let postData = try NSJSONSerialization.dataWithJSONObject(parameters, options: NSJSONWritingOptions.PrettyPrinted)
        postD = postData;
    }catch let error as NSError{
        print(error.description)
    }
    var request = NSMutableURLRequest(URL: NSURL(string: "\(hostUrl)/users")!,
                                      cachePolicy: .UseProtocolCachePolicy,
                                      timeoutInterval: 10.0)

    request.HTTPMethod = "POST"
    request.allHTTPHeaderFields = headers
    request.HTTPBody = postD

    let session = NSURLSession.sharedSession()
    let dataTask = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
        if (error != nil) {
            print(error)
        } else {
            let httpResponse = response as? NSHTTPURLResponse
            statuscode = httpResponse!.statusCode
            print("AFTER HTTP RESPONSE STATUSCODE = \(statuscode)")

        }
    })

    dataTask.resume()
    return statuscode
}
我正在调用这个函数:

let statuscode = client.register(parameters)
        print("denemeeeeeeeeeeeeee\(statuscode)")

这是我的输出

denemeeeeeeeeeeeeee0
AFTER HTTP RESPONSE STATUSCODE = 400

2 个答案:

答案 0 :(得分:1)

register是一个异步API,因此我担心在数据请求完成之前,您的函数将始终返回。您必须考虑其他方法。例如,您的grep -E '^.{0,31}(47225|82115)' file 方法可能会收到您在完成数据任务时调用的完成闭包。

答案 1 :(得分:0)

您需要实现callback函数来处理这类内容:

func register(parameters : [String : String], callback: ((success: Bool, response: NSHTTPURLResponse)->Void){

let headers = [
    "content-type": "application/json",
    "cache-control": "no-cache",
]
var postD = NSData();
do {
    let postData = try NSJSONSerialization.dataWithJSONObject(parameters, options: NSJSONWritingOptions.PrettyPrinted)
    postD = postData;
}catch let error as NSError{
    print(error.description)
}
var request = NSMutableURLRequest(URL: NSURL(string: "\(hostUrl)/users")!,
                                  cachePolicy: .UseProtocolCachePolicy,
                                  timeoutInterval: 10.0)

request.HTTPMethod = "POST"
request.allHTTPHeaderFields = headers
request.HTTPBody = postD

let session = NSURLSession.sharedSession()
let dataTask = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
    if (error != nil) {
        print(error)
    } else {

        let httpResponse = response as? NSHTTPURLResponse
        callback?(success: true, response: httpResponse)


    }
})

dataTask.resume()
}

并在你的函数调用中:

client.register(parameters, callback: { (success, response) in
     var statuscode = 0
     statuscode = response!.statusCode
     print("AFTER HTTP RESPONSE STATUSCODE = \(statuscode)")
})

如果这还不足以实现您想要做的事情,您还可以查看dispatch groups

祝你好运。