Sunday, 15 June 2014

How to return NSDictionary in swift, while waiting for its value to be set? -


Please leave me (: string, baseurl: url string) to speed up new

My problem was that I could not return my NSDictionary. This is my function

  is the personal function request - & gt; NSDictionary {var dict: NSDictionary! Request on request = HTTPTask () request.requestSerializer = HTTPRequestSerializer () request.requestSerializer.headers [headerKey] = getToken () // a header value request.baseURL = baseurl request.GET (URL, example by adding parameters: zero, success: Error on NSData {go str = NSString? (Data: Encoding: NSUTF8StringEncoding) if the data in response to the above data = response.responseObject {(Response: HttpResponse): NSError dict = NSJSONSerialization.JSONObjectWithData (Data, Options: NSJSONReadingOptions.MutableContainers , Error:; :)}}, failure and NSDictionary println ("\ (dict response)": error) {(error: NSError, response: HttpResponse) println  

return dict {} but when I read println ("Reaction: \ "Dict \"), my data is logged under the function.

I think my function returns a blank object because the GET method is running in a different thread and is waiting for a response.

Can anyone help me, no one will comment.

You are okay, the request runs in asynchronous form in another thread, Suggestion using handler is.

  Private func request (url: string, berserl: string, complete: (result: nsDictionary) -> zero) {var dict: NSDictionary! Request on request = HTTPTask () request.requestSerializer = HTTPRequestSerializer () request.requestSerializer.headers [headerKey] = getToken () // a header value request.baseURL = baseurl request.GET (URL, example by adding parameters: zero, success: Error on NSData {go str = NSString? (Data: Encoding: NSUTF8StringEncoding) if the data in response to {data: response: response.responseObject in {(response: HttpResponse): NSError dict = NSJSONSerialization.JSONObjectWithData (data, options: NSJSONReadingOptions.MutableContainers Error:; :) Completion (result: dict)}}, Failure: Error in NSDictionary println ("\ (dict response)") Root: NSError, response: HttpResponse) in println ("error: \ (error)") completion (result: zero) // This is not the best choice, the better error handler will return the error}}}  

For more information see:


No comments:

Post a Comment