如何在Swift中解决这个错误?

时间:2017-07-29 06:28:11

标签: ios swift swift3

我在控制台中收到以下错误:

  

致命错误:在解包可选值时意外发现nil

在Xcode编辑器中显示如下错误:

  

THREAD 1 EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0 * 0)

我在Swift 3中有这个代码用于调用API并在视图中加载它:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell
    cell.nameLabel!.text = nameArray[indexPath.row]
    cell.dobLabel!.text = dobArray[indexPath.row]
    cell.descLabel!.text = descArray[indexPath.row]

    /*
    let imgURL = NSURL(string: imgURLArray[indexPath.row])
    let data = NSData(contentsOf: (imgURLArray as? URL)!)
    cell.imageView!.image = UIImage(data: data as! Data)
    */

    return  cell
}

这是我的TableViewCell文件:

class TableViewCell: UITableViewCell {

    @IBOutlet weak var nameLabel: UILabel?
    @IBOutlet weak var descLabel: UILabel?
    @IBOutlet weak var dobLabel: UILabel?

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
}

1 个答案:

答案 0 :(得分:1)

尝试修改你的ApiViewController并检查我的代码然后看看会发生什么

 import UIKit

 class ApiViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    final let urlString = "http://microblogging.wingnity.com/JSONParsingTutorial/jsonActors"
    var nameArray = [String]()
    var dobArray = [String]()
    var imgURLArray = [String]()
    var descArray = [String]()
     var actorarray = NSArray()
    @IBOutlet weak var tableView: UITableView!

    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? NSDictionary {
            print(jsonObj!.value(forKey: "actors")!)

            actorarray = jsonObj!.value(forKey: "actors") as? NSArray

            self.tableView.reloadData()

        }
    }).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!)
        })
    }

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return actorarray.count
 }

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell

    let dic = self.actorarray[indexPath.row] as! NSDictionary


    cell.nameLabel!.text = dic.object(forKey: "name") as! String
    cell.dobLabel!.text = dic.object(forKey: "dob") as! String
    cell.descLabel!.text = dic.object(forKey: "image") as! String

    return  cell
 }
}