URL Variable is not being recognized using NSURL

262 views Asked by At

I am attempting to parse a website, however when I was simply starting to set up my code I got the error, playMusicViewController.swift does not have a member named 'url', as well as the error expected declaration under the task.resume, I did include an import statement as well.

let url = NSURL(string: "http://www.mp3juices.cc/")

let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
    println(NSString(data: data, encoding: NSUTF8StringEncoding))
}

task.resume()
1

There are 1 answers

0
Leo Dabus On BEST ANSWER

You just need to move those lines inside viewDidLoad or create a method with them:

override func viewDidLoad() {
    super.viewDidLoad()

    let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
        println(NSString(data: data, encoding: NSUTF8StringEncoding))
    }

    task.resume()
}