When i call this function , function returns value and then doing all other things. How can i make function to return value end of other things.
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
}
Im calling this function like this :
let statuscode = client.register(parameters)
print("denemeeeeeeeeeeeeee\(statuscode)")
And this is my output
denemeeeeeeeeeeeeee0
AFTER HTTP RESPONSE STATUSCODE = 400