NSUserDefaults. setValue works, Not setBool

前端 未结 4 1677
春和景丽
春和景丽 2021-01-18 02:16

I trying to store some settings in NSUserDefaults, but It seems that the app won\'t store the setBool values.

This works:

[[NSUserDefaults standardUs

4条回答
  •  不思量自难忘°
    2021-01-18 02:48

    I had the exact same problem. Everything EXCEPT BOOLs were persisting correctly; but i was using some old coding styles from ios 3. recoded this way, everything works.

    If anybody else is using old books.... here is an example

    Bad stuff:

    //////////// set / get bL2R
    if (![[NSUserDefaults standardUserDefaults]
          boolForKey:kL2RomanizationChoice]) {
        [[NSUserDefaults standardUserDefaults]
         setBool:YES
         forKey:kL2RomanizationChoice];
        bL2R = YES;
        NSLog(@"L2Rom not found, set to YES.");
    }
    else {
        bL2R   = [[NSUserDefaults standardUserDefaults]
                  boolForKey:kL2RomanizationChoice];
        NSLog(@"L2Rom found.");
        if (bL2R) {
            NSLog(@"L2Rom found to be YES.");
        }
    
    }
    

    Good stuff:

    if (![defaults boolForKey:kL2RomanizationChoice])
        [defaults setBool:YES forKey:kL1RomanizationChoice];
    
    L2String_Setting = [defaults objectForKey:kSecondLangChoice];
    bL2R = [defaults boolForKey:kL2RomanizationChoice];
    

    Update: sadly this only seemed to work briefly, and now is failing again... using Xcode 4.5.2. may just swap out bools for integers...

提交回复
热议问题