1

I've got an app that used a multi value menu in the settings bundle to configure the rate at which the on-screen map is refreshed. The panel is set up like this:

enter image description here

When I run the app on my phone, I immediately go to the settings app and the menu reads "1 sec" as I expect. When I return to my app and attempt to initiate the map update the app crashes with "fatal error: unexpectedly found nil while unwrapping an Optional value" and highlights an EXC_BREAKPOINT, when I step back through the code I am taken to the settings for my multi value menu. Also, if I go to the settings app first and manually re-selct the same value the app runs perfectly. The highlighted code looks like this:

enter image description here

Have I incorrectly set up the default value?

user3185748
  • 2,478
  • 8
  • 27
  • 43
  • Can you show the code where you get the fatal error? – tng Jan 05 '15 at 22:45
  • Edited the original post with a screenshot. – user3185748 Jan 05 '15 at 22:49
  • You'll need to go to the source line with the error. Go up the stack and show the line of code (your source code) where the error occurs. – tng Jan 05 '15 at 22:52
  • Now, if I press the 'step out' button it just flashes the file I last had open (so originally my settings bundle but the last time it flashed the app delegate) and then returns to the same line no matter how many times I press it. – user3185748 Jan 05 '15 at 22:53
  • Don't step out - just double click on the frame. From your screen shot, double click on the "1 FlightTracker..." frame under "Thread 1". As an aside, basic operational skills of the debugger will help you in your journey. I recommend looking at this article - http://www.raywenderlich.com/10209/my-app-crashed-now-what-part-1 – tng Jan 05 '15 at 22:56
  • Thanks for the tutorial, I'll be sure to take a look! The offending code is the following `timer = NSTimer.scheduledTimerWithTimeInterval(((NSUserDefaults.standardUserDefaults().stringForKey("mapUpdate")! as NSString).doubleValue), target: self, selector: aSelector, userInfo: nil, repeats: true)` where I initiate the update of the map every interval set in the settings. (Should default to 1 second) – user3185748 Jan 05 '15 at 23:01
  • Based on the fact that there is an error unwrapping the optional, I bet that NSUserDefaults.standardUserDefaults.stringForKey("mapUpdate") is nil. You should double check that. And if that's the case, did you configure the plist file properly? – tng Jan 05 '15 at 23:10
  • Sure enough, it returns nil on launch. I imagine it's something to do with the 'Default Value' field of my root.plist since when I change the settings in the app it runs through. Apple's documentation defines this field as: `The default value for the preference key. This value is returned when the specified preferences key (represented by the Key entry) is not present in the preferences database. This key is required.` Am I not using it in this manner? – user3185748 Jan 05 '15 at 23:16
  • Did you register your plist for user defaults? See this: http://stackoverflow.com/questions/8473986/how-to-set-initial-values-for-nsuserdefault-keys – tng Jan 05 '15 at 23:22
  • Well after a bit of digging I managed to register my defaults in AppDelegate although the item is no longer selected on install. For now though, it does the job. Thank you so much for the help! – user3185748 Jan 05 '15 at 23:35
  • No problem - I'll write a summary up as the answer. Good luck! – tng Jan 05 '15 at 23:37

1 Answers1

1

Based on the fact that there is an error unwrapping the optional, I bet that NSUserDefaults.standardUserDefaults.stringForKey("mapUpdate") is nil. You should double check that.

If that's the case, you'll need to register your plist file for use with NSUserDefaults. See How to set initial values for NSUserDefault Keys? for defailts on how to do that.

Community
  • 1
  • 1
tng
  • 4,286
  • 5
  • 21
  • 30