【问题标题】:JSON TableView Swift ErrorJSON TableView Swift 错误
【发布时间】:2015-10-23 00:14:39
【问题描述】:

我正在尝试使用此 JSON URL 填充 tableview。我验证了 URL,它工作正常。我的代码有问题。我有一个“Repository.swift”文件,里面有一个类。这是我的代码:我无法解决问题。

var repositories = [Repository]()

override func viewDidLoad() {
    super.viewDidLoad()

    let reposURL = NSURL(string: "https://www.kimonolabs.com/api/7flcy3qm?apikey=gNq3hB1j0NtBdAvXJLEFx8JaqtDG8y6Y")!

    if let JSONData = NSData(contentsOfURL: reposURL) {
        if let json = NSJSONSerialization.JSONObjectWithData(JSONData, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary {
            if let reposArray = json["items"] as? [NSDictionary] {
                for item in reposArray {
                    repositories.append(Repository(json: item))
                }
            }
        }
    }
}

func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return self.repositories.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

    cell.textLabel?.text = repositories[indexPath.row].Event
    cell.detailTextLabel?.text = repositories[indexPath.row].Hasta

    return cell
}

【问题讨论】:

  • 调用可以抛出但没有标记'try'并且错误未处理
  • 3 个错误 1)'let' 声明不能是计算属性 2) 不能下标 NSDictionary 类型的值? 3)“是”测试总是正确的..感谢您的努力:)

标签: ios json swift tableview


【解决方案1】:

你需要添加try catch

我修复了你的代码

override func viewDidLoad() {
    super.viewDidLoad()

    let reposURL = NSURL(string: "https://www.kimonolabs.com/api/7flcy3qm?apikey=gNq3hB1j0NtBdAvXJLEFx8JaqtDG8y6Y")!

    if let JSONData = NSData(contentsOfURL: reposURL) {
        do{
            let json = try NSJSONSerialization.JSONObjectWithData(JSONData, options: .MutableContainers)
            if let reposArray = json["items"] as? [NSDictionary] {

                for item in reposArray {

                    // repositories.append(Repository(json: item))


                }
            }
        }catch {
        //do whatever
        }

    }
}

请尝试一下。

【讨论】:

  • 3 个错误 1)'let' 声明不能是计算属性 2) 不能下标 NSDictionary 类型的值? 3)“是”测试总是正确的..感谢您的努力:)
  • thanks.. 那位现在似乎还可以......但它导致了另一个错误:“定义与以前的值冲突”与此行:覆盖 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
  • @DonDraper 那是因为你的代码没有正确的右括号},请尝试解决这个问题,如果你愿意,把你的整个视图控制器,我会尽力帮助
  • 修正了括号:此行可能不可变:“for item in reposArray {”
  • @DonDraper 那是因为您没有在 for 中使用变量 item。要么使用变量,要么将其替换为_
猜你喜欢
  • 2016-01-24
  • 1970-01-01
  • 1970-01-01
  • 2014-12-30
  • 1970-01-01
  • 2012-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多