开发者

Bad Access when saving to standardUserDefaults

开发者 https://www.devze.com 2023-04-02 10:10 出处:网络
For some reason, when I try to save one string to NSUserDefaults, I get a bad access error. NSString *apikey = @\"key\";

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

精彩评论

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