I am trying to implement In App Purchases, and I am tracking which purchases a user has purchased via NSUserDefaults
. I have a function that sets the values of each purchase, but when it runs, I get an error saying that I am mutating the dictionary of purchase values even though the dictionary is declared with a var
instead of a let
and is an NSMutableDictionary
. Sometimes it does work, but most of the time it doesn't. I get a few warnings about declaring my variables with let
instead of var
, but I ignore them to give my variables maximum mutability. Why does this not work?
The error I get is:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'
Code:
static func setPurchased(purchase:PurchaseID, value:Bool)
{
let defaults = NSUserDefaults.standardUserDefaults()
if (defaults.objectForKey(PURCHASES_KEY) == nil)
{
initializePurchases() // go set some initial values
}
if var purchases = (defaults.objectForKey(PURCHASES_KEY) as? NSMutableDictionary) // WARNING: should be declared with let because not mutated
{
print("setting purchase \(purchase.id()) to \(value)")
var key = purchase.id() // WARNING: should be declared with let because not mutated
purchases[key] = value // CRASH HERE
defaults.setObject(purchases, forKey:PURCHASES_KEY)
defaults.synchronize()
}
}