簡體   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