我想将数据库中的name列插入到字典中,然后对其进行排序并将部分添加到tableView中。 我有一个函数只存储数据库中的名称并返回MutableArray和另一个返回字典数组的函数。 我不知道为什么但是我的MutableArray总是为零,我设法获取值的唯一方法是当我尝试对字典进行排序并获得类型的返回值时:[String,[String]]而不是[String:[String]]
我的代码:
let sharedInstance = ModelManager()
class ModelManager: NSObject {
var database: FMDatabase? = nil
class func getInstance() -> ModelManager
{
if(sharedInstance.database == nil)
{
sharedInstance.database = FMDatabase(path: Util.getPath("test3.sqlite"))
}
return sharedInstance
}
func getAllCrimesNames() -> NSMutableArray { // Part1: database into mutableArray
sharedInstance.database!.open()
let resultSet: FMResultSet! = sharedInstance.database!.executeQuery("SELECT * FROM CrimeTable", withArgumentsIn: nil)
let marrCrimesInfo : NSMutableArray = NSMutableArray()
if (resultSet != nil) {
while resultSet.next() {
let crimesInfo : NameInfo = NameInfo()
crimesInfo.Name = resultSet.string(forColumn: "Name")
marrCrimesInfo.add(crimesInfo)
}
}
sharedInstance.database!.close()
return marrCrimesInfo
}
}
第2部分:将我的mutableArray放入字典并对其进行排序
class SectionData {
var marrCrimesNames : NSMutableArray!
func getCrimesNames() {
marrCrimesNames = NSMutableArray()
marrCrimesNames = ModelManager.getInstance().getAllCrimesNames()
}
func getSectionsFromData() -> [String: [String]] {
var sectionDictionary = [String: [String]]()
if marrCrimesNames != nil { // for some reason always nil!
let crime: NameInfo = marrCrimesNames.object(at: 0) as! NameInfo
_ = crime.Name
var firstLetter: [String] = []
for crime in marrCrimesNames {
firstLetter.append((crime as AnyObject).Name)
}
let characters = Array(Set(firstLetter.flatMap({ $0.characters.first })))
for character in characters.map({ String($0) }) {
sectionDictionary[character] = firstLetter.filter({ $0.hasPrefix(character) })
}
//var sortedSectionDictionary = Array(sectionDictionary.keys).sorted() // return [String, [String]] instead of [String: [String]]
}
return sectionDictionary
}
}
我知道很多,但我错过了一些简单的东西,我真的需要帮助。 谢谢大家...
答案 0 :(得分:1)
对字典进行排序没有意义。字典是无序的。
我的猜测是,您希望表的数据由已排序的部分数组表示。每个部分都可以由一个带有两个字段的结构表示,一个用于第一个字母,另一个用于该部分中的名称数组。
您可以使用以下代码将sectionDictionary
(将第一个字母映射到名称数组)转换为已排序的剖面结构数组:
struct CrimeNameSection {
let firstLetter: String
let names: [String]
}
func getSectionsFromData() -> [CrimeNameSection] {
// make sure marrCrimesNames is non-nil
guard marrCrimesNames != nil else {
return []
}
// add each name to dictionary mapping first letters to name arrays
var sectionDictionary = [String : [String]]()
for crime in marrCrimesNames {
let crime = crime as! NameInfo
let firstChar = String(crime.Name[crime.Name.startIndex])
if var names = sectionDictionary[firstChar] {
names.append(crime.Name)
sectionDictionary[firstChar] = names
} else {
sectionDictionary[firstChar] = [crime.Name]
}
}
// convert sectionDictionary to sorted array of CrimeNameSections
let sections = sectionDictionary.map { (key, value) in
return CrimeNameSection(firstLetter: key, names: value)
}
let sortedSections = sections.sorted { $0.firstLetter < $1.firstLetter }
return sortedSections
}