【问题标题】:Get values from complex Array of Dictionary in Swift2从 Swift2 中的复杂字典数组中获取值
【发布时间】:2015-10-22 09:08:01
【问题描述】:

我将字典显示为 UITableView。我在 Objective-c 中很容易做到这一点。但现在很快,我无法从字典数组中获取值。当我尝试在tableView:cellForRowAtIndexPath 中获取值时,预编译器会抛出类似

的错误

无法转换“AnyObject?!”类型的值到指定类型'数组'

或者像下面这个例子一样

无法转换“AnyObject?!”类型的值idnex 类型为“int”

var cellArray = []

override func viewDidLoad() {
    cellArray = [
        [
            "section" : "Melody",
            "rows" :
                [
                    [
                        "title" : "Wi-Fi / Pairing",
                        "icon" : "",
                        "action" : "actionConnectSpeaker"
                    ],
                    [
                        "title" : "Sound",
                        "icon" : "",
                        "action" : "actionConnectSpeaker"
                    ]
                ]
        ],

        [
            "section" : "Music",
            "rows" :
                [
                    [
                        "title" : "Add a new account",
                        "icon" : "",
                        "action" : "actionAddAccount"
                    ]
                ]
        ]
    ]
}

...

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = UITableViewCell(style: UITableViewCellStyle.Value2, reuseIdentifier: nil)

    cell.textLabel?.text = cellArray[indexPath.section]["rows"][indexPath.row]["title"]


    return cell
}

我的结论是我们不能这样做......我找到任何例子。我可能对 Swift 中的类型不太了解...

【问题讨论】:

    标签: arrays swift dictionary swift2


    【解决方案1】:

    在 Swift 中,如果你想调用像 objectAtIndex:objectForKey: 这样的函数,你必须向下转换 AnyObject 类型。

    let section = cellArray[indexPath.section]
    let rows = section["rows"] as! [[String:String]]
    cell.textLabel?.text = rows[indexPath.row]["title"]
    

    【讨论】:

      【解决方案2】:

      如果您确定没有 nil 值,只需执行此操作:

      cell.textLabel?.text = cellArray[indexPath.section]["rows"]![indexPath.row]["title"]
      

      【讨论】:

      • 检查上面的答案。您的答案回答了另一个问题
      【解决方案3】:

      如果你想安全地做到这一点(如果任何对象可以是 nil 或其他类型,你应该使用if let

      if let dict = cellArray[indexPath.section] as? [String : AnyObject],
          rows = dict["rows"] as? [AnyObject], 
          result = rows[indexPath.row] as? [String : AnyObject],
          title = result["title"] {
          cell.textLabel?.text = title
      }
      

      【讨论】:

        【解决方案4】:

        在 Swift 中,您通常应该避免使用包含不同类型元素的集合。您的部分数据是一个字典,“部分”键的值是一个字符串,而“行”键的值是一个数组。尝试用元组替换这个字典。

        var cellArray = [(String, [[String: String]])]()
        
        override func viewDidLoad() {
            cellArray = [
                (
                    "Melody",
                    [
                        [
                            "title" : "Wi-Fi / Pairing",
                            "icon" : "",
                            "action" : "actionConnectSpeaker"
                        ],
                        [
                            "title" : "Sound",
                            "icon" : "",
                            "action" : "actionConnectSpeaker"
                        ]
                    ]
                ),
        
                (
                    "Music",
                    [
                        [
                            "title" : "Add a new account",
                            "icon" : "",
                            "action" : "actionAddAccount"
                        ]
                    ]
                )
            ]
        }
        
        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
            let cell = UITableViewCell(style: UITableViewCellStyle.Value2, reuseIdentifier: nil)
        
            cell.textLabel?.text = cellArray[indexPath.section].1[indexPath.row]["title"]
        
            return cell
        }
        

        这样 Swift 的类型系统可以帮助您确保使用正确的类型,不需要强制向下转换。

        【讨论】:

          猜你喜欢
          • 2017-01-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多