如何从 nsdictionary 中获取选定值的键
how to get key of selected value from nsdictionary
我正在使用 NSDictionary
从 json 获取数据,然后我将 NSDictionary
的值添加到 UITableView
。当用户选择 table cell 它将发回 value.The 点是如何从 table cell in NSDictionary
in Swift?
中获取所选值的键
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.
}
}
感谢@Martin R,他告诉我answer.I现在用那个代码得到了这个
let keys = (myFruits as NSDictionary).allKeysForObject("Sunkist")
println(String(keys[0] as! NSString))
输出为
0_22
这段代码可以正常工作。
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"))
我正在使用 NSDictionary
从 json 获取数据,然后我将 NSDictionary
的值添加到 UITableView
。当用户选择 table cell 它将发回 value.The 点是如何从 table cell in NSDictionary
in Swift?
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.
}
}
感谢@Martin R,他告诉我answer.I现在用那个代码得到了这个
let keys = (myFruits as NSDictionary).allKeysForObject("Sunkist")
println(String(keys[0] as! NSString))
输出为
0_22
这段代码可以正常工作。
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"))