1

I've been searching around to learn how to get some data using NSURLSession in Swift. There is a general pattern being repeated on the internet for making an http request using NSURLSession, and it looks a lot like this:

let url = NSURL(string: "myPretendURLthatIsNotThisURL.com")
let task = NSURLSession.sharedSession().dataTaskWithURL(url) {(data, response, error) in println(NSString(data: data, encoding: NSUTF8StringEncoding))}
task.resume()

If I place a real URL in, I can see a lot of JSON output printed, which is the expected response from the server. Nice : ) I can see the data from println(). However, I would rather display the returned data in a text view when I press a button. I do believe my solution relies on using a delegate (somehow). Is it all right to request an example of how this is done, in the following context:

  1. Code is in Swift
  2. Press a button (IBAction, contains the code above to make the request)
  3. Display the data returned (it's JSON, I'll worry how to parse it later on) in a text view.

Thanks!

H_H
  • 605
  • 1
  • 9
  • 13
  • Exchange the code after the `in` keyword with the code to fill the text view. And read some introduction books. – dasdom Nov 30 '14 at 07:56
  • i am also having problems with NSURLSession - i don't understand how to get data to some variable – moonvader Jan 07 '15 at 14:48

0 Answers0