ios - How to get sorted Dictionary from MutableArray using swift3 -
i manage create dictionary database add sections in tableview
.
my function database array , array dictionary (a: aname1,aname2, b: bname1,bname2...). when sorted dictionary return value type [string, [string]] , need [string: [string]], when don't use sort action it's give error:
thread1:exc_bas_instruction(code=exc_i386_invop,subcode=0x0)
in console:
fatal error: unexpectedly found nil while unwrapping optional value (lldb)
my code:
class sectiondata { var marrcrimesnames : nsmutablearray! func getcrimesnames() { marrcrimesnames = nsmutablearray() marrcrimesnames = modelmanager.getinstance().getallcrimesnames() } func getsectionsfromdata() -> [string: [string]] { var sectiondictionary = [string: [string]]() let crime: crimesinfo = (marrcrimesnames.object(at: 0) as! crimesinfo) // thread1:exc_bas_instruction(code=exc_i386_invop,subcode=0x0) _ = 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 = sectiondictionary.sorted(by: { $0.key < $1.key }) // sort action return [string, [string]] return sectiondictionary } }
the error assume value return in: let crime: crimesinfo = (marrcrimesnames.object(at: 0) as! crimesinfo)
set.
the error show not set , return nil
. check it:
if let crime = marrcrimesnames.object(at: 0) { // here can safely use crime ;) }
Comments
Post a Comment