-1

I try use shared session to talk to a http server. In xcode playground,

import Foundation

var session = NSURLSession.sharedSession()

var url = NSURL(string: "http://api.zoomeye.org/user/login")

var _data: NSData?
var _response: NSURLResponse?
var _error: NSError?

var dataTask = session.dataTaskWithURL(url!, completionHandler: {(data, response, error) in
    _data = data
    _response = response
    _error = error
})

dataTask.resume()

print(_data)
print(_response)
print(_error)

the printed results are nil, it shouldn't be nil.

Use chrome to access the url, message in entity body is this,

{"url": "https://www.zoomeye.org/api/doc", "message": "request failed, the specified method was not allowed", "error": "method_not_allowed"}

Where are mistakes? and how to fix it?

attolee
  • 602
  • 1
  • 9
  • 22

1 Answers1

0

Move the code that is supposed to run when the data is available into the completion black. That's what it's for.

Duncan C
  • 128,072
  • 22
  • 173
  • 272