感谢帮助人员,我有一本字典数组的字典。 我的目标是从一个内部字典值中挑选出一个从plist文件中读取的数组。值为String类型,并返回具有重复值的数组。此返回的数组总共有30个值(元素),可以按6个值进行分类。我需要以某种方式创建这6个值的数组,以便我可以在我的tableView数据源上使用它,numberOfSectionsInTableView:
我查看了map()和filter(),但无法解决任何问题。我的代码是;
// setupLoading called from viewDidLoad()
func setupLoading() {
let path = NSBundle.mainBundle
let bundlePath = path.pathForResource("KempfRef", ofType: "plist")
let dict: NSDictionary? = NSDictionary(contentsOfFile: bundlePath!)
let plistArray: NSArray = dict!["KempfRef"] as NSArray
for innerDict in plistArray as Array {
let kempf: KempfFiber = KempfFiber() // Model
kempf.name = innerDict["Name"] as String
kempf.detail = innerDict["Detail"] as String
kempf.imageName = innerDict["ImageName"] as String
kempf.specGrav = innerDict["SpecGrav"] as String
kempf.toughness = innerDict["Toughness"] as String
kempf.pdfFile = innerDict["PDF"] as String
//println("DESCRIPTION: \(kempf.description())")
var theType = innerDict["Type"] as String
var kempfTypes = [String](arrayLiteral: theType)
println("theType")
}
//这是theType的println 类型是:
BERKempf
BERKempf
BERKempf
BERKempf
CHRYBERKempf
CHRYBERKempf
CHRYBERKempf
CORUNKempf
CORUNKempf
CORUNKempf
CORUNKempf
GARNKempf
GARNKempf
GARNKempf
GARNKempf
GARNKempf
GARNKempf
GARNKempf
OTHERKempf
OTHERKempf
OTHERKempf
OTHERKempf
OTHERKempf
OTHERKempf
OTHERKempf
SPINEKempf
SPINEKempf
SPINEKempf
SPINEKempf
谢谢你们
答案 0 :(得分:0)
我会创建另一个字典,其中包含一个字符串键(将是您的类型)和一个KempfFiber数组作为值。然后当你得到一个类型时,你从字典中检索数组。如果它是nil然后创建一个新数组,将kempf对象添加到它,否则只需将kempf对象添加到现有数组。然后,您可以将键作为阵列的数组获取。
这样的事情(kempfData
和sectionKeys
是当前班级的属性 -
self.kempfData=[String:[KempfFiber]]
for innerDict in plistArray as Array {
let kempf: KempfFiber = KempfFiber() // Model
kempf.name = innerDict["Name"] as String
kempf.detail = innerDict["Detail"] as String
kempf.imageName = innerDict["ImageName"] as String
kempf.specGrav = innerDict["SpecGrav"] as String
kempf.toughness = innerDict["Toughness"] as String
kempf.pdfFile = innerDict["PDF"] as String
var theType = innerDict["Type"] as String
var kempfArray=self.kempfData[theType]
if (kempfArray ==nil) {
kempfArray =[Kempf]()
}
kempfArray!.append(kempf)
self.kempfData[theType]= kempfArray
}
self.sectionKeys=Array(self.kempfData.keys)
现在您可以返回sectionKeys.count
作为您的部分计数。
您的numberOfRowsInSection
将是
let sectionData=self.kempfData[self.sectionKeys[indexPath.section]]
if (sectionData != nil) {
return sectionData!.count
}
else {
return 0
}
同样,在您cellForRowAtIndexPath
中,您可以使用
let sectionData=self.kempfData[self.sectionKeys[indexPath.section]]
if (sectionData != nil) {
let kempf=sectionData[indexPath.row]
// Configure your cell...
}
else {
// Something bad happened
}
您可能希望在创建self.sectionKeys
数组后对其进行排序,以便您的部分按字母顺序排列。