Xcode 8 个测试版 6 个 AnyObject Swift 3 个更改

Xcode 8 beta 6 AnyObject Swift 3 changes

Xcode beta 6 改变了一些 Swift 语言

Since ‘id’ now imports as ‘Any’ rather than ‘AnyObject’, you may see errors where you were previously performing dynamic lookup on ‘AnyObject’.

我已经尝试过在执行动态查找之前显式转换为 AnyObject 或强制转换为特定对象类型的修复程序

但我不确定我做的是否正确 - 请有人帮忙这里是来自 Beta 5 的原始工作代码

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! SpecialCell
        let maindata = values[(indexPath as NSIndexPath).row]
        cell.name.text = maindata["name"] as? String
        cell.id.text = maindata["id"] as? String
        //team_id.text = maindata["team_id"] as? String

        return cell
    }

https://www.dropbox.com/s/ln0vx3b9rbywv83/Screen%20Shot%202016-08-18%20at%2014.32.23.png?dl=0

根据 Beta 6 发行说明,您必须(桥接)转换为 AnyObject

cell.name.text = (maindata["name"] as AnyObject) as? String

或强制施法

cell.name.text = maindata["name"] as! String

这是比普通词典更喜欢具有不同 属性 类型的自定义 类 / 结构的又一原因。

我需要改变我的方法并放弃 NSMutableArray(我很高兴)

所以我声明空数组如下

var values = [[String:AnyObject]]()

然后像现在这样将数据弹出到其中

values = try! JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! [[String : AnyObject]];

另一个小调整

let maindata = values[(indexPath).row]

工作完成 - 感谢@vadian 跳到聊天帮助我理解他的回答,这在技术上是正确的