EXC_BAD_ACCESS with NSUserdefaults on iphone

I have the following code in an ApplicationDelegate application. My deployment goal is 3.0 and higher, however, I get EXC_BAD_ACCESS when I run the application with the following code on my iPhone with 3.1.3, however, on the simulator with 4.2 it works fine.

This problem is now SOLVED . The code below works and it got EXC_BAD_ACCESS due to NSURLConnection in my code. I will not delete my code if anyone else gets this problem. Thank you for your help.

UPDATE: File plist:

<dict>
<key>StringsTable</key>
<string>Root</string>
<key>PreferenceSpecifiers</key>
<array>
    <dict>
        <key>Type</key>
        <string>PSGroupSpecifier</string>
        <key>Title</key>
        <string>Check for report on start?</string>
    </dict>
    <dict>
        <key>Type</key>
        <string>PSToggleSwitchSpecifier</string>
        <key>Title</key>
        <string>Autocheck reports?</string>
        <key>Key</key>
        <string>FAutoUpdatePrefKey</string>
        <key>DefaultValue</key>
        <true/>
    </dict>
</array>

Applicationdelegate

+ (void)initialize {

NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *pListPath = [path stringByAppendingPathComponent:@"Settings.bundle/Root.plist"];

NSDictionary *pList = [NSDictionary dictionaryWithContentsOfFile:pListPath];

NSMutableArray *prefsArray = [pList objectForKey:@"PreferenceSpecifiers"];
NSMutableDictionary *regDictionary = [NSMutableDictionary dictionary];

for (NSDictionary *dict in prefsArray) {
    NSString *key = [dict objectForKey:@"Key"];
    if(key) {
        id value = [dict objectForKey:@"DefaultValue"];
        [regDictionary setObject:value forKey:key];
    }
}
[[NSUserDefaults standardUserDefaults] registerDefaults:regDictionary];
}
+3
source share
3 answers

registerDefaults: iOS 2.0 (. ), . , , regDictionary , .

. - , . , , : . ,

, ( PCheese)

- (void)registerDefaultsFromSettingsBundle {
    NSString *settingsBundle = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"];
    if(!settingsBundle) {
        NSLog(@"Could not find Settings.bundle");
        return;
    }

    NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:[settingsBundle stringByAppendingPathComponent:@"Root.plist"]];
    NSArray *preferences = [settings objectForKey:@"PreferenceSpecifiers"];

    NSMutableDictionary *defaultsToRegister = [[NSMutableDictionary alloc] initWithCapacity:[preferences count]];
    for(NSDictionary *prefSpecification in preferences) {
        NSString *key = [prefSpecification objectForKey:@"Key"];
        if(key) {
            [defaultsToRegister setObject:[prefSpecification objectForKey:@"DefaultValue"] forKey:key];
        }
    }

    [[NSUserDefaults standardUserDefaults] registerDefaults:defaultsToRegister];
    [defaultsToRegister release];
}

UPDATE: , pList, PSToggleSwitchSpecifier <true/> <string>YES</string>

+1

registerDefaults .

for (NSDictionary *dict in prefsArray) {
       NSString *key = [dict objectForKey:@"Key"];
       if(key) {
           id value = [dict objectForKey:@"DefaultValue"];
           [[NSUserDefaults standardUserDefaults] setObject:value forKey:key]; //<-change               
       }
}
0

The problem here is not the difference between the OS, but the difference between the device and the simulator. The simulator uses the Mac OS X file system, which is not case sensitive, but the file system on iOS.

So, you can double check @ "Settings.bundle / Root.plist" and @ "PreferenceSpecifiers" if their flask lock is correct.

0
source

Source: https://habr.com/ru/post/1783280/


All Articles