The iOS app I develop consists two storyboards called Main.storyboard and Explore.storyboard which are merged using a UIBarButtonItem via a Storyboard Reference.
Main.StoryBoard UIBarButtonItem
Explore.storyboard consists a UIViewController where its class functions to call a REST API using json. Following is the code:
import UIKit
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
final let urlString = "http://tvshowapi.azurewebsites.net/tvshownewsfeed"
@IBOutlet weak var tableView: UITableView!
var nameArray = [String]()
var favouriteCountArray = [String]()
var imgURLArray = [String]()
var contentArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
self.downloadJsonWithURL()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func downloadJsonWithURL() {
let url = NSURL(string: urlString)
URLSession.shared.dataTask(with: (url as? URL)!, completionHandler: {(data, response, error) -> Void in
if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [[String:Any]] {
for tvshow in jsonObj! {
if let name = tvshow["screenName"] as? String {
self.nameArray.append(name)
}
if let cnt = tvshow["favouriteCount"] as? Int {
self.favouriteCountArray.append("\(cnt)")
}
if let image = tvshow["imageUrl"] as? String {
self.imgURLArray.append(image)
}
if let content = tvshow["content"] as? String {
self.contentArray.append(content)
}
}
OperationQueue.main.addOperation({
self.tableView.reloadData() // CRASHES HERE
})
}
}).resume()
}
func downloadJsonWithTask() {
let url = NSURL(string: urlString)
var downloadTask = URLRequest(url: (url as? URL)!, cachePolicy: URLRequest.CachePolicy.reloadIgnoringCacheData, timeoutInterval: 20)
downloadTask.httpMethod = "GET"
URLSession.shared.dataTask(with: downloadTask, completionHandler: {(data, response, error) -> Void in
let jsonData = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments)
print(jsonData!)
}).resume()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return nameArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell
cell.nameLabel.textAlignment = .left
cell.nameLabel.text = nameArray[indexPath.row]
cell.dobLabel.text = favouriteCountArray[indexPath.row]
let imgURL = NSURL(string: imgURLArray[indexPath.row])
if imgURL != nil {
let data = NSData(contentsOf: (imgURL as? URL)!)
cell.imgView.image = UIImage(data: data as! Data)
}
return cell
}
///for showing next detailed screen with the downloaded info
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "DetailViewController") as! DetailViewController
vc.imageString = imgURLArray[indexPath.row]
vc.nameString = nameArray[indexPath.row]
vc.dobString = favouriteCountArray[indexPath.row]
vc.contentString = contentArray[indexPath.row]
self.navigationController?.pushViewController(vc, animated: true)
}
}
At the runtime an error terminates the program with :
fatal error: unexpectedly found nil while unwrapping an Optional value
What am I doing wrong? What can I do to eliminate this error?