-1

I am calling Url which will give me Json in get() function. I am calling get() function from another class and try to return result of Json in Array format. but it shows Found null error on return statement . when I tried to print values of Json it writing correctly. This is my code in swift.

 func get() -> NSArray
        {
            let postEndpoint: String =  "Link_For_JSON_Data"
            let session = NSURLSession.sharedSession()
            let url = NSURL(string: postEndpoint)!
            var jsonArray : NSArray?
            var jsonArray1 : NSArray?

            session.dataTaskWithURL(url, completionHandler: { ( data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
                // Make sure we get an OK response
                guard let realResponse = response as? NSHTTPURLResponse where
                    realResponse.statusCode == 200 else
                {
                        print("Not a 200 response")
                        return
                }

                // Read the JSON
                do
                {
                    if let contentString = NSString(data:data!, encoding: NSUTF8StringEncoding)
                    {
                        // Print what we got from the call
                        jsonArray = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSArray
                        print("jsonArray here", jsonArray)
                        // Update the label

                        dispatch_async(dispatch_get_main_queue())
                            { () -> Void in
                            self.getDataFormREST(jsonArray!)
                            }

                    }
                }

                catch
                {
                    print("bad things happened")
                }

            }).resume()

       return jsonArray!
        }

     func getDataFormREST(resultArray: NSArray) //-> NSArray
        {
            //        let resultDictionary = resultArray[(searchDetails)!-1] as! NSDictionary
            testArray = resultArray
            print("TESTArray ON ",testArray)
    }
Komal Kamble
  • 424
  • 1
  • 8
  • 25
  • Possible duplicate of [Downloading JSON with NSURLSession doesn't return any data](http://stackoverflow.com/questions/35289389/downloading-json-with-nsurlsession-doesnt-return-any-data) – Eric Aya Mar 03 '16 at 10:38

1 Answers1

0

You can't write a function that does an async call and then returns the results as the function result. That's not how async code works. Your function queues up the async dataTaskWithURL request, and then returns before it has even had a chance to send the request, much less receive the results.

You have to rewrite your get() function to be a void function (no result returned) but take a completion block. Then, in your data task's completion handler you get the data from the jsonArray and call the get() function's completion block, passing it the jsonArray.

See this project I posted on Github that illustrates what I'm talking about:

SwiftCompletionHandlers on Github

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