6

NSUserDefaults doesn't seem to work for me in iOS8. When I run a simple code it crashes with this error pointing to the line with the setDouble call:

Thread 1: EXC_BAD_ACCESS(code=1, address=0xffffffffc)

the code:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
  let defs = NSUserDefaults.standardUserDefaults()
  defs.setDouble(2.5, forKey: "foo")

  return true
}

What am I doing wrong?

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
gerhard
  • 427
  • 1
  • 4
  • 12
  • 3
    Declare as a var not let. Let is constant, once you declare you can't change it. – modusCell Jul 09 '14 at 14:23
  • Device or Simulator? I cannot reproduce the problem on the iOS 8 Simulator. – Martin R Jul 09 '14 at 14:43
  • 3
    @mohacs: `let defs = …` should be no problem. `defs` is a *reference type* and not modified by calling a method on the instance. See "Classes are Reference Types" in the Swift book. – Martin R Jul 09 '14 at 14:46
  • @MartinR yes you are correct. since the beta released I had couple of problems like this, declared Ref Type as constant with let keyword then couldn't access methods. replaced let with var and it worked. May be because it is early beta and bug? – modusCell Jul 09 '14 at 14:53
  • 1
    It works with `var` and with `let` in my iOS 8 Simulator. – Martin R Jul 09 '14 at 14:56
  • I tried on device, now I ran it on simulator and it worked there. Not sure what's up with my phone (xcode and ios are both on beta 3). Yes, `let` should not be a problem. – gerhard Jul 09 '14 at 15:46
  • yes let work in my iOS8 simulator, i am using xcode 6 beta 2 – karthikPrabhu Alagu Jul 10 '14 at 07:19

1 Answers1

0

I was getting this same error when I first upgraded to beta 3 but solved it by cleaning my project.

Try 'Product - Clean'

Worked for me though no idea what the real problem was.

Magnas
  • 3,832
  • 5
  • 33
  • 48
  • this is a bug in the ios8 simulator. It works correctly on physical ios8 devices. See http://stackoverflow.com/questions/24985825/nsuserdefaults-not-cleared-after-app-uninstallation/25086816#25086816 – anders Aug 29 '14 at 18:15