ios - Swift NSUserDefaults not saving Dictionary -
here code. have been solving bug month still not solved.
here codes.
var color : string = "" var colorsentdictionary = dictionary<string, int>() override func viewwillappear(animated: bool) { super.viewwillappear(animated) // load colorsentdictionary self.loadcolorsentdictionary() } func loadcolorsentdictionary () { // load total messages sent var defaults : nsuserdefaults = nsuserdefaults.standarduserdefaults() if (defaults.dictionaryforkey("colorsentdictionary") != nil) { var colorsentdictionary : nsdictionary = defaults.dictionaryforkey("colorsentdictionary")! self.colorsentdictionary = colorsentdictionary dictionary } } func savecolorsentdictionary () { // save total messages received var defaults : nsuserdefaults = nsuserdefaults.standarduserdefaults() var colorsentdictionary : nsdictionary = self.colorsentdictionary defaults.setobject(colorsentdictionary, forkey: "colorsentdictionary") defaults.synchronize() if (defaults.dictionaryforkey("colorsentdictionary") != nil) { println("it not nil !!!!!!!!!") } else { println("it nil") } } @ibaction func sendmessage(sender : anyobject) { self.color = "yellow" println("the color of message is: \(self.color)") // add value messagesentdictionary if (self.colorsentdictionary["\(self.color)"] != nil) { var colorsentcount : int = self.colorsentdictionary[self.color]! self.colorsentdictionary["\(self.color)"] = colorsentcount+1 println("it exists") } else { self.colorsentdictionary["\(self.color)"] = 1 println("not exist") } self.savecolorsentdictionary() self.navigationcontroller!.poptorootviewcontrolleranimated(true) }
whenever trigger save method, colorsentdictionary not saved. log prints "it nil" cannot find key "colorsentdictionary".
but if put self.savecolorsentdictionary () in viewwillappear, dictionary saved , log prints "it not nil !!!!!!!!".
i think problem weird don't think bug since tested in official release version of xcode 6. can please solve me? :)
Comments
Post a Comment