ios - How to get one column from my database into dictionary using Swift3 -
i want insert name column database dictionary sort , add sections tableview. have function store names database , return mutablearray , function return array of dictionary. don't know why mutablearray nil, way i'v managed values when try sorted dictionary , got return value of type : [string, [string]] instead of [string: [string]]
my code:
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 mutablearray sharedinstance.database!.open() let resultset: fmresultset! = sharedinstance.database!.executequery("select * 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 } }
part 2: mutablearray dictionary , sort it
class sectiondata { var marrcrimesnames : nsmutablearray! func getcrimesnames() { marrcrimesnames = nsmutablearray() marrcrimesnames = modelmanager.getinstance().getallcrimesnames() } func getsectionsfromdata() -> [string: [string]] { var sectiondictionary = [string: [string]]() if marrcrimesnames != nil { // reason nil! let crime: nameinfo = marrcrimesnames.object(at: 0) as! nameinfo _ = crime.name var firstletter: [string] = [] crime in marrcrimesnames { firstletter.append((crime anyobject).name) } let characters = array(set(firstletter.flatmap({ $0.characters.first }))) 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 }
}
i know it's lot i'm missing simple , need here. thank all...
it doesn't make sense sort dictionary. dictionaries unordered.
my guess want table's data represented sorted array of sections. each section represented struct 2 fields, 1 first letter , 1 array of names in section.
you can convert sectiondictionary
, maps first letters arrays of names, sorted array of section structs using code this:
struct crimenamesection { let firstletter: string let names: [string] } func getsectionsfromdata() -> [crimenamesection] { // make sure marrcrimesnames non-nil guard marrcrimesnames != nil else { return [] } // add each name dictionary mapping first letters name arrays var sectiondictionary = [string : [string]]() 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 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 }
Comments
Post a Comment