/ / Comment puis-je obtenir les données de NSURLSession.sharedSession (). DataTaskWithRequest - swift, nsurlsession

Comment puis-je obtenir les données de NSURLSession.sharedSession (). DataTaskWithRequest - swift, nsurlsession

class PostFOrData {
let url = NSURL( string: "http://210.61.209.194:8088/SmarttvWebServiceTopmsoApi/GetReadlist")
var picUrl = NSURL(string : "http://210.61.209.194:8088/SmarttvMedia/img/epi00001.png")
var responseString : NSString = ""

func forData() -> NSString {

let request = NSMutableURLRequest( URL: url!)
request.HTTPMethod = "POST"
var s : NSString = ""

let postString : String = "uid=59"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)

let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in

if error != nil {
println("error=(error)")
return
} else {
println("response = (response!)")
self.responseString = NSString(data: data, encoding: NSUTF8StringEncoding)!
println("responseString = (self.responseString)")
}

}

// I want to return NSString here, but I always get nothing

return self.responseString

}
}

Quelqu'un sait comment obtenir les données d'une tâche?

Réponses:

32 pour la réponse № 1

Vous ne pouvez pas renvoyer de données directement à partir d’une tâche asynchrone.

La solution avec Swift 2 est de créer un gestionnaire d’achèvement comme ceci:

class PostFOrData {
// the completion closure signature is (NSString) -> ()
func forData(completion: (NSString) -> ()) {
if let url = NSURL(string: "http://210.61.209.194:8088/SmarttvWebServiceTopmsoApi/GetReadlist") {
let request = NSMutableURLRequest( URL: url)
request.HTTPMethod = "POST"
let postString : String = "uid=59"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if let data = data,
jsonString = NSString(data: data, encoding: NSUTF8StringEncoding)
where error == nil {
completion(jsonString)
} else {
print("error=(error!.localizedDescription)")
}
}
task.resume()
}
}
}


let pfd = PostFOrData()

// you call the method with a trailing closure
pfd.forData { jsonString in
// and here you get the "returned" value from the asynchronous task
print(jsonString)
}

De cette façon, la fin n'est appelée que lorsque la tâche asynchrone est terminée. C’est un moyen de "renvoyer" les données sans utiliser réellement return.

Swift 3 version

class PostFOrData {
// the completion closure signature is (String) -> ()
func forData(completion:  @escaping (String) -> ()) {
if let url = URL(string: "http://210.61.209.194:8088/SmarttvWebServiceTopmsoApi/GetReadlist") {
var request = URLRequest(url: url)
request.httpMethod = "POST"
let postString : String = "uid=59"
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request) {
data, response, error in
if let data = data, let jsonString = String(data: data, encoding: String.Encoding.utf8), error == nil {
completion(jsonString)
} else {
print("error=(error!.localizedDescription)")
}
}
task.resume()
}
}
}


let pfd = PostFOrData()

// you call the method with a trailing closure
pfd.forData { jsonString in
// and here you get the "returned" value from the asynchronous task
print(jsonString)
}