繁体   English   中英

如何在swift中访问字典数组

[英]How to access array of dictionaries in swift

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

这有什么不对吗?

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

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

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

写下面的字典,

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

解决分段错误:11错误,

试试这个,转到Build Settings - > Swift Compiler - Code generation,将Optimization Level设置为None。

希望这会帮助你。

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
}

暂无
暂无

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

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