我刚刚学习了如何从CoreData数据库中检索属性键并将其放入数组中。除了每次方法运行时,它每次都以不同的顺序生成数据集外,一切正常。我现在只有3个键-名称,性别,类型。
是否可以做一些事情来使每次以相同的顺序遍历键?我将提供更多属性,并使用该信息填充表格视图,因此我希望它保持一致。
["type","sex","name"]
["goat","F","Bob"]
["sex","name","type"]
["M","Jack","goat"]
["name","type"]
["Bob","goat"]
["type","sex"]
["goat","M"]
["name","goat"]
func generateAttributeList(_ animal:Animal) -> Array<String> {
let dictAttributes = animal.entity.attributesByName
var arrAttributeTitles:Array<String> = []
for (key,_) in dictAttributes {
arrAttributeTitles.append(key)
}
return arrAttributeTitles
}
//makes the Value Array
func generateValueList () {
for (name,_) in animal!.entity.attributesByName {
let animalValue = animal!.value(forKey: name) as! String
childValues.append(animalValue)
}
//var attributesByName: [String : NSAttributeDescription] { get }
}