smartsanja
smartsanja

Reputation: 4540

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

Upvotes: 3

Views: 1249

Answers (3)

gotnull
gotnull

Reputation: 27214

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
}

Upvotes: 0

Jigar Tarsariya
Jigar Tarsariya

Reputation: 3237

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.

Upvotes: 1

Jayesh Miruliya
Jayesh Miruliya

Reputation: 3317

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

Upvotes: 1

Related Questions