NSUserDefault give EXC_BAD_ACCESS trying to get values

1.1k Views Asked by At

I'm using NSUserDefaults in my app to store some flag values. My function increases the value each time that a condition has occurred. Occasionally it works, but most of the time it crashes and get an EXC_BAD_ACCESS message.

var sample1: Int = countWord.integer(forKey: "countWord1")
var sample2: Int = countWord.integer(forKey: "countWord2")
var sample3: Int = countWord.integer(forKey: "countWord3")

Those are the lines where I get the error.

EDIT

This is how I initialize and setcountword

let countWord = UserDefaults.standard
countWord.set(sample1+1, forKey: "countWord1")

Application crashes after repeatedly iterated function

1

There are 1 best solutions below

1
On

I don't think these are the lines your app is crashing. For exc-bad-access you usually have to trace way back. You can use Instrument to trace it.

The variables defined in the controller and methods are well handled by ARC. This usually happens when you have two controller, for example, ViewController A is trying to access ViewController B's strong variable after ViewController B is deallocated.