What am I missing...?
elsewhere in my project there is code like this:
let allUsers = ["userName":["difficulty": 1, "highscore": 50],"userName2":["difficulty": 2, "highscore: 75]] defaults.setObject(allUsers, forKey: "allUsers") I want to change a value for one user in that array of users:
var allUsers = defaults.objectForKey("allUsers") as! [String:NSMutableDictionary] let changingUser = allUsers["userName"]! as NSMutableDictionary Neither of these will work:
changingUser.setObject(3, forKey: "difficulty") changingUser["difficulty"] = 3 with the error:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'
objectForKeywill be immutable. You can't simply downcast an immutable dictionary to a mutable dictionary. You need to create a new NSMutableArray from the array returned from user defaults, modify that and then write it back.let allUsers: [String:NSMutableDictionary] = .... Your cast to NSMutableDictionary would be correct at that point.