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] setOb开发者_开发技巧ject:apikey forKey:@"apiKey"];
did the trick as a replacement for the line that was causing the exception.
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?
精彩评论