如何在swift中访问字典数组

时间:2016-06-17 05:17:47

标签: ios arrays swift dictionary

我的array包含dictionaries。这是我尝试访问数组的方式。

这是错的吗?

let aWeather: Dictionary = arrWeatherDeatils[indexPath.row]! as!Dictionary

当我使用此代码时,xcode显示此错误:

Command failed due to signal: Segmentation fault: 11

3 个答案:

答案 0 :(得分:1)

let aWeather: Dictionary<AnyObject,AnyObject> = arrWeatherDeatils[indexPath.row]! as! Dictionary<AnyObject,AnyObject>

答案 1 :(得分:1)

写下面的字典,

 let aWeather : NSDictionary = arrWeatherDeatils.objectAtIndex(indexPath.row) as! NSDictionary

解决分段错误:11错误,

试试这个,转到Build Settings - &gt; Swift编译器 - 代码生成,将优化级别设置为无。

希望这会对你有所帮助。

答案 2 :(得分:0)

var arrWeatherDeatils = [
    "Sunny": 76.0,
    "Chilly": 22.1,
    "Warm": 37.0
]

var typeList: [String] {
    get {
        return Array(arrWeatherDeatils.keys)
    }
}

然后在cellForRowAtIndexPath

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    //note I did not check for nil values. Something has to be really         let row = indexPath.row //get the array index from the index path
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
    let myRowKey = typeList[row] //the dictionary key
    cell.textLabel!.text = myRowKey
    let myRowData = arrWeatherDeatils[myRowKey] //the dictionary value
    cell.detailTextLabel!.text = String(format: "%6.3f",myRowData!)
    return cell
}