Alex
Alex

Reputation: 167

Bad Access when saving to standardUserDefaults

For some reason, when I try to save one string to NSUserDefaults, I get a bad access error.

NSString *apikey = @"key";
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
[defaults setObject:apikey forKey:@"apiKey"];
[defaults synchronize];

The line that it gives bad access on is [defaults setObject:apikey forKey:@"apiKey"];

I haven't released either apikey or defaults, yet it always gives me bad access. I am wondering how to get around this.

EDIT: SOLVED

It seems that [[NSUserDefaults standardUserDefaults] setObject:apikey forKey:@"apiKey"]; did the trick as a replacement for the line that was causing the exception.

Upvotes: 0

Views: 577

Answers (1)

Are you really sure in your real code it does not start with:

NSString *apikey = "key";

Instead of an objective-C string? Or that it's not initialized in some other way? When you run Analyse, does it give warnings for any lines of code?

Upvotes: 1

Related Questions