i'm new to Swift, i allready learned the basic syntax and i'm trying to do more advanced stuff. My first attempt with the code bellow is to do an HTTP request on the URL http://www.test.com and fetch the response:
//: Playground - noun: a place where people can play
import UIKit
var url : NSURL = NSURL(string: "http://www.test.com")!
var request: NSURLRequest = NSURLRequest(URL: url)
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config)
var responseString:NSString?;
let task : NSURLSessionDataTask = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
print("toto")
if error != nil {
print("error=\(error)")
return
} else {
print("response = \(response!)")
responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)!
print("responseString = \(responseString)")
}
});
task.resume()
print (responseString)
When i run the code in Xcode, "toto" isn't printed and responseString is nil, any idea on what's going wrong ?