我在Swift中遇到一些麻烦在Objective C中很容易。也就是说,在填充单元格之前,在字段“title”上将plist排序为字母顺序。
整个'排序'过程看起来好像在Swift中应该更容易,但我只是没有得到它。我已经拖网了但没有得到解决方案。任何帮助非常感谢。 (我不会把所有代码都放在这里因为它是多余的)。
override func viewDidLoad() {
super.viewDidLoad()
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
let sourcePath = documentsPath.stringByAppendingPathComponent("MyAnnotationsUSA.plist")
if let myMuseums = NSArray(contentsOfFile: sourcePath as String){
for item in myMuseums {
titleData.append(item.objectForKey("title") as! String)
subTitleData.append(item.objectForKey("subtitle") as! String)
stateData.append(item.objectForKey("state") as! String)
etc.etc......
答案 0 :(得分:0)
看起来你过早地解除了数据的关联。怎么样:
var data: [(String, String, String)] = []
if let myMuseums = NSArray(contentsOfFile: sourcePath as String){
for item in myMuseums {
data.append((item.objectForKey("title") as! String, item.objectForKey("subtitle") as! String, item.objectForKey("state") as! String))
}
}
data.sortInPlace({$0.0 < $1.0})
titleData = data.map({$0.0})
subTitle = data.map({$0.1})
stateData = data.map({$0.2})
......并观看那些被迫展开的人......!
答案 1 :(得分:0)
在SO上有一些很好的帮助并解决了这个问题。我正在从服务器下载pList,因此它不会保存在mainBundle中。这就是解决方案(原谅我用来分隔不同元素的空白区域):
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
let sourcePath = documentsPath.stringByAppendingPathComponent("MyAnnotationsUSA.plist")
if let content = NSArray(contentsOfFile: sourcePath as String){
let descriptor = NSSortDescriptor(key: "title", ascending: true)
let myMuseum = content.sortedArrayUsingDescriptors([descriptor])
for item in myMuseum{
titleData.append(item.objectForKey("title") as! String)
subTitleData.append(item.objectForKey("subtitle") as! String)