I'm fetching JSON data online and converting them to NSArray, and to String and class Arrays to manage the data. However, for some reason, the code exits the GetData() method after the line let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
I've also verified that there was no issues with fetching the data, but it just can't be displayed on the table.
I've attached my project file also for download.
Codes from ViewController.swift
import UIKit
class ViewController: UIViewController, UITableViewDataSource {
var nameList = [NameManager]()
@IBOutlet weak var NameTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
GetData()
// Do any additional setup after loading the view, typically from a nib.
NameTable.dataSource = self
NameTable.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func GetData(){
let session = NSURLSession.sharedSession()
let request = NSMutableURLRequest(URL: NSURL(string: "http://www.json-generator.com/api/json/get/bPfifKWNaq?indent=2")!)
request.HTTPMethod = "GET"
let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
if let error = error {
print(error)
}
if let data = data{
do{
let resultJSON = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions())
let resultArray = resultJSON as? NSArray
for jsonObjectString in resultArray!{
let code = jsonObjectString["code"] as! String
let name = jsonObjectString["name"] as! String
let description = jsonObjectString["description"] as! String
self.nameList.append(NameManager(code: code, name: name, description: description))
}
self.nameList.count
}catch _{
print("Received not-well-formatted JSON")
}
}
if let response = response {
let httpResponse = response as! NSHTTPURLResponse
print("response code = \(httpResponse.statusCode)")
}
})
task.resume()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
let count = nameList.count
return count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let myCell = NameTable.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as UITableViewCell
myCell.textLabel?.text = nameList[indexPath.row].name
myCell.detailTextLabel?.text = nameList[indexPath.row].description
return myCell
}
}
Codes from NameManager.swift
import Foundation
class NameManager{
var code:String
var name:String
var description:String
init(code: String, name: String, description: String){
self.code = code
self.name = name
self.description = description
}
}
dataTaskWithRequestis asynchron. This means the block is called on a background thread.return counttoreturn 2instead. But my program failed to executemyCell.textLabel?.text = nameList[indexPath.row].nameDo you mind downloading from the link and try change?