我无法正确理解如何将这个转换为Swift。能有人帮忙吗?显然我需要提高我的Objective-C 理解程度 :(
var animals : [String : [String]] =
["B" : ["Bear", "Black Swan", "Buffalo"],
"C" : ["Camel", "Cockatoo"],
"D" : ["Dog", "Donkey"],
"E" : ["Emu"],
"G" : ["Giraffe", "Greater Rhea"],
"H" : ["Hippopotamus", "Horse"],
"K" : ["Koala"],
"L" : ["Lion", "Llama"],
"M" : ["Manatus", "Meerkat"],
"P" : ["Panda", "Peacock", "Pig", "Platypus", "Polar Bear"],
"R" : ["Rhinoceros"],
"S" : ["Seagull"],
"T" : ["Tasmania Devil"],
"W" : ["Whale", "Whale Shark", "Wombat"]]
var animalSection = [String]()
var rev = [String]()
var animalIndexTitles = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"];
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a ni
animalSection = animals.keys.array
rev = sorted(animalSection, { (s1, s2) -> Bool in
return s1 <= s2
})
println(rev)
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return animalSection.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var sectionTitle = rev[section] // String
var sectionAnimals : [String] = animals[sectionTitle]! // String Array
return sectionAnimals.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: AnyObject = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
var sectionTitle = rev[indexPath.section]
var sectionAnimals : [String] = animals[sectionTitle]!
var animal = sectionAnimals[indexPath.row]
cell.textLabel.text = animal
return cell as UITableViewCell
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return rev[section]
}
func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject]! {
return animalIndexTitles
}
func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int
{
return 0
}
这里充满了Swift代码,请注意:
由于Swift在某些方面比面向对象更具有功能性(而数组是结构体,不是对象),因此使用函数“find”来操作数组,该函数返回一个可选值,因此请准备处理nil值: