简体   繁体   中英

NSURL completion event is not being called

I'm currently starting to learn swift. My first "test-app" is a simple application, which does a NSURL Session request on startup and displays the received data in a tableview. So far, my tableView does work and also my request method runs without any compilation errors. My problem now is, that the event handler is not being called:

func doRequest(url: String) -> String
{
    var url = NSURL(string: url);
    var request = NSURLRequest(URL: url!);
    var config = NSURLSessionConfiguration.defaultSessionConfiguration();
    var session = NSURLSession(configuration: config);
    var task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
        println("CALLED");
    });
    return "";
}

The above method doesn't print anything into the console. Since using google did not help me to find a solution, I try my luck here. What's the issue with my code? Am I doing something wrong without realising it?

The URL for my tests is: api.football-data.org/alpha/soccerseasons/354/leagueTable which returns a simple json object.

Any help is appreciated!

Add task.resume() to start the download. After you create the task, you must start it by calling its resume method.

var task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
    println("CALLED");
});
task.resume()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM