Reputation: 5106
I am using NSDictionary
to get data from json and then i added the value from NSDictionary
to UITableView
.When user is selected one of the table cell it will send back the value.The point is how to get the key of selected value from 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.
}
}
Upvotes: 0
Views: 975
Reputation: 403
This code will work fine.
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"))
Upvotes: 2
Reputation: 5106
Thanks for @Martin R who give me that answer.I got this now with that code
let keys = (myFruits as NSDictionary).allKeysForObject("Sunkist")
println(String(keys[0] as! NSString))
Output is
0_22
Upvotes: 2