简体   繁体   中英

How to access array of dictionaries in swift

I have array which contains dictionaries . 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:

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,

Try this, go to Build Settings -> Swift Compiler - Code generation, set Optimisation Level to 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

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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