我正在使用NSDictionary
从json获取数据,然后我将NSDictionary
的值添加到UITableView
。当用户被选中其中一个表格单元格时,它会发回值。关键是如何从Swift中NSDictionary
的表格单元中获取所选值的键?
import UIKit
class ViewController: UIViewController {
var myFruits = ["0_22":"Sunkist","34_22":"Pine Apple","45_22":"Grape","1_123":"Apple"]
override func viewDidLoad() {
super.viewDidLoad()
// If my selected key is "Sunkist, what do i need to to get its value (0_22)"
// Any Code Help?
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:2)
感谢@Martin R,他给了我答案。我现在用这个代码获得了这个
let keys = (myFruits as NSDictionary).allKeysForObject("Sunkist")
println(String(keys[0] as! NSString))
输出
0_22
答案 1 :(得分:2)
此代码可以正常使用。
var myFruits = ["0_22":"Sunkist","34_22":"Pine Apple","45_22":"Grape","1_123":"Apple"]
var dic:NSDictionary = NSDictionary(dictionary:myFruits)
println(dic.allKeysForObject("Sunkist"))