简体   繁体   English

如何在swift中访问字典数组

[英]How to access array of dictionaries in swift

I have array which contains dictionaries . 我有包含dictionaries array This is how I try to access the array. 这是我尝试访问数组的方式。

Whats the wrong of this? 这有什么不对吗?

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

When I use this code, xcode shows this error: 当我使用此代码时,xcode显示以下错误:

Command failed due to signal: Segmentation fault: 11
let aWeather: Dictionary<AnyObject,AnyObject> = arrWeatherDeatils[indexPath.row]! as! Dictionary<AnyObject,AnyObject>

Write dictionary like below, 写下面的字典,

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

To solve segmentation fault:11 error, 解决分段错误:11错误,

Try this, go to Build Settings -> Swift Compiler - Code generation, set Optimisation Level to None. 试试这个,转到Build Settings - > Swift Compiler - Code generation,将Optimization Level设置为None。

Hope this will help you. 希望这会帮助你。

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

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

Then in your cellForRowAtIndexPath 然后在你的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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM