开发者

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] 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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜