1

I am trying to call a json webservice in swift, With the following code and display it in tableview in swift IOS.

/*declared as global*/ var IdDEc = [String]() // string array declared globally

//inside viewdidload

let url = NSURL(string: "http://192.1.2.3/PhpProject1/getFullJson.php")

let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in

let json1 = NSString(data: data!, encoding: NSUTF8StringEncoding)

print("json string is = ",json1) // i am getting response here

let data = json1!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)

    do {

        let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as! NSArray

            for arrayData in json {

                let notId = arrayData["ID"] as! String

                self.IdDEc.append(notId)
            }

            print(" id = ",IdDEc) //here i am getting values

        } catch let error as NSError {

            print("Failed to load: \(error.localizedDescription)")
        }

        print(" id  out count = ",self.IdDEc.count) //here also
    }

    print(" id  out count = ",self.IdDEc.count) // not here

    task.resume()

i declared the array IdDEc as global, still the scope of that array resides inside NSURLSession only,

Also i want to use this array to populate tableview. Here is the sample json output file

[

{"ID":"123" , "USER":"philip","AGE":"23"},

{"ID":"344","USER":"taylor","AGE":"29"},

{"ID":"5464","USER":"baker","AGE":"45"},

{"ID":"456","USER":"Catherine","AGE":"34"}

]

I am a newbee in swift please help

Laffen
  • 2,753
  • 17
  • 29
Mukund
  • 1,107
  • 2
  • 18
  • 40
  • 1
    Data is not ready yet. Notice that you are running an async request. – Desdenova May 20 '16 at 07:54
  • @Desdenova oh is it?, so please help me to catch the data, I am new in swift, so where is the post execute of the asynctask how to catch the data – Mukund May 20 '16 at 08:00
  • Swift doesn't have a post execute method like java. In the scope you can access the data. You should move on from there. – Desdenova May 20 '16 at 08:02
  • @Desdenova So is there a method to populate the data in tableview? – Mukund May 20 '16 at 08:03
  • Yes of course, but it's also very different from java's adapters. You should reload the table view when you have the data and if you set up table view data source delegate's right, you should be all set. – Desdenova May 20 '16 at 08:05
  • @Desdenova Sorry i am not so much familier in swift i need code, I got this from this https://stackoverflow.com/questions/33344069/ios-calling-webservice-and-parsing-json-in-swift# page can you give me some links to tutotrials like this one or give me code to complete thsi – Mukund May 20 '16 at 08:08

1 Answers1

3

The idea is to use a "callback".

Here, I've made one for the NSArray you want to get:

completion: (dataArray: NSArray)->()

We create a function to get the array, and we add this callback to the function's signature:

func getDataArray(urlString: String, completion: (dataArray: NSArray)->())

and as soon as the array is ready we'll use the callback:

completion(dataArray: theNSArray)

Here's how the complete function could look like:

func getDataArray(urlString: String, completion: (dataArray: NSArray)->()) {
    if let url = NSURL(string: urlString) {
        NSURLSession.sharedSession().dataTaskWithURL(url) {(data, response, error) in
            if error == nil {
                if let data = data,
                    json1 = NSString(data: data, encoding: NSUTF8StringEncoding),
                    data1 = json1.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) {
                    do {
                        let json = try NSJSONSerialization.JSONObjectWithData(data1, options: [])
                        if let jsonArray = json as? NSArray {
                            completion(dataArray: jsonArray)
                        }
                    } catch let error as NSError {
                        print(error.localizedDescription)
                    }
                } else {
                    print("Error: no data")
                }
            } else {
                print(error!.localizedDescription)
            }
        }.resume()
    }
}

Now we use this function like this, no more asynchronous issues:

getDataArray("http://192.1.2.3/PhpProject1/getFullJson.php") { (dataArray) in
    for dataDictionary in dataArray {
        if let notId = dataDictionary["ID"] as? String {
            self.IdDEc.append(notId)
        }
    }
    print("id out count = ", self.IdDEc.count)
}

Swift 3 update + fixes and improvements.

func getContent(from url: String, completion: @escaping ([[String: Any]])->()) {
    if let url = URL(string: url) {
        URLSession.shared.dataTask(with: url) { (data, response, error) in
            if error == nil  {
                if let data = data {
                    do {
                        let json = try JSONSerialization.jsonObject(with: data, options: [])
                        if let content = json as? [[String: Any]] { // array of dictionaries
                            completion(content)
                        }
                    } catch {
                        // error while decoding JSON
                        print(error.localizedDescription)
                    }
                } else {
                    print("Error: no data")
                }
            } else {
                // network-related error
                print(error!.localizedDescription)
            }
        }.resume()
    }
}

getContent(from: "http://192.1.2.3/PhpProject1/getFullJson.php") { (result) in
    // 'result' is what was given to 'completion', an array of dictionaries
    for dataDictionary in result {
        if let notId = dataDictionary["ID"] as? String {
            // ...
        }
    }
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253