【发布时间】:2015-05-26 11:10:17
【问题描述】:
我正在尝试开发一个 UIPickerView,其中只有值数组。所以我将有一个 Dictionary(venue) 来存储 json 中的键和值,还有一个 Array(venueValue) 只存储值。通过使用venueValue,我可以在UIPickerView 中显示结果。但是,我想要用户从picker 中选择的值的键。不幸的是,我正在考虑如何从我的json 中获取Dictionary(key:value) 和Array(key)。有什么帮助吗?
var venue = [String:String]()
var venueValue = [String]()
var selectedValue : String? // to show in text box after picker selected and to get key of selected value from venue
func populateVenues(completion : (error: NSError?,value : [String]?,dict : [String:String]) -> Void){
var request = NSMutableURLRequest(URL: NSURL(string: "http://localhost:8080/xxx/xxx/xxx")!)
var session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"
var err: NSError?
let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
println("Auction : Request Completed")
var err: NSError?
if error == nil{
var venues = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as! NSDictionary
for (k, v) in venues {
if let value = v as? [String] {
println(value)
// Acutally value should start from "All Venue".I wonder why it start at "ZeroCentral",Please check my output.
self.venueValue.append(value[0])
}
}
//I still dont get the dictionary yet.how to change NSDictionary to Dictionary
// What i want is to store value in venueValue and to store both key and value in venue because when the picker can only store array.So i gonna get the selected value from picker and get the key of selected value from dictionary(venue)
completion(error: nil,value: venueValue,dict:venues)
}
else{
println(error.localizedDescription)
}
if(err != nil) {
// If there is an error parsing JSON, print it to the console
println("JSON Error \(err!.localizedDescription)")
}
})
task.resume()
}
这是我的场地输出。
{
0 = (
"All Venues"
);
"100_152" = (
"ORIX Fukuoka"
);
"101_164" = (
"ORIX IP Stock"
);
"102_131" = (
"ORIX Kobe"
);
"103_135" = (
"ORIX Nagoya"
);
"104_132" = (
"ORIX Sendai"
);
"105_54" = (
"SAA Hamamatsu"
);
"106_29" = (
"SAA Sapporo"
);
"107_184" = (
Sakura
);
}
我尝试输出的场地值没有像场地输出那样排序
[ORIX Nagoya]
[ORIX IP Stock]
[ORIX Kobe]
[All Venues]
[ORIX Fukuoka]
…...
为什么它不像旧的?如果我想从“所有场地”开始,怎么做?任何帮助?请我如此接近答案。
【问题讨论】:
标签: ios arrays swift nsdictionary