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
See Question&Answers more detail:os