Saturday, 15 September 2012

ios - App is crashing if NSUserDefaults returns nil -


I have a condition from which the key value will be displayed NSUserDefaults memory (restored) Look in the storyboard, but they are only stored in the next view controller (saved), where they will be restored. In case the application crashes with nothing error, when I try to run the scene with the included store key, (such as when the app is run for the first time) is stored.

Fatal error: Unexpectedly, when opening an optional value, zero is found

Further, NSUserDefaults are part of the switch statement on this pre-view , As follows: Is there no way to tell the system that if the key of the key is zero, please display 0. I think this is the specifics of Swift, but it does not explain how it is syntax in this condition. Appreciate the help.

Var Dummy: Dummy Test! = No switch equal to self.lessonNumber {Case 1:. NSUserDefaults.standardUserDefaults () setObject (self.score, forKey: "storedDummyEssentialsPoints") Case 2:. NSUserDefaults.standardUserDefaults () setObject (self.score, forKey: "storedDummyFoodPoints") Case 3: NSUserDefaults.standardUserDefaults () setObject (self.score, forKey: "storedDummyNumbersPoints").

...

check whether the NSUserDefaults value any Value or not. If not, set values ​​are in the form of:

  str on: AnyObject? = NSUserDefaults.standardUserDefaults () objectForKey ("storedDummyEssentialsPoints") if (str == zero) {NSUserDefaults.standardUserDefaults () setObject. (Self.score, forKey: "storedDummyEssentialsPoints")}.  

No comments:

Post a Comment