在展开可选的swift 3时发现nil

时间:2019-07-09 05:04:24

标签: swift tableview

URL JSON格式如下:

[{"sp_name":"Tanjong Didih, Langkawi"},{"sp_name":"Kg. Ayer Hangat, Langkawi"},{"sp_name":"Selat Panchor"}]

这是我的代码:

//
//  SecondViewController.swift
//  tabview
//
//  Created by on 06/07/2019.
//  Copyright © 2019  All rights reserved.
//

import UIKit
import Foundation
import WebKit

class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var cang: UITableView!
    @IBAction func refresh(sender: AnyObject) {
        get()
    }

    var values: NSArray = []

    override func viewDidLoad() {
        super.viewDidLoad()

        get()
        // 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 get(){
        let url = NSURL(string: "http://josi.com/test.php")

        let data = NSData(contentsOfURL: url!)  

        values = try! NSJSONSerialization.JSONObjectWithData(data!, options: []) as! NSArray
        cang.reloadData()


    }

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = cang.dequeueReusableCellWithIdentifier("BasicCell", forIndexPath: indexPath)
        let maindata = values[indexPath.row]

        cell.textLabel?.text = maindata["negeri"] as? String


        return cell
    }


}

我想创建数据表来查看结果。但是我得到了这个日志"fatal error: unexpectedly found nil while unwrapping an Optional value"。请帮助我。

0 个答案:

没有答案