iPhone 突然发现,setting配置文件中的DefaultValue可能拿不了值
按照我的想法,如果用户没有在setting设置值且有default-value,那么应该获取的是default-value,因为打开setting也能看见这些值.但是今天突然发现好像不是那么会事.获取的是null.看了AppPrefs的代码,它也进行了判断.如下:
?
- (void)applicationDidFinishLaunching:(UIApplication *)application{[window addSubview:[navigationController view]];NSString *testValue = [[NSUserDefaults standardUserDefaults] stringForKey:kFirstNameKey];if (testValue == nil){// no default values have been set, create them here based on what's in our Settings bundle info//NSString *pathStr = [[NSBundle mainBundle] bundlePath];NSString *settingsBundlePath = [pathStr stringByAppendingPathComponent:@"Settings.bundle"];NSString *finalPath = [settingsBundlePath stringByAppendingPathComponent:@"Root.plist"];NSDictionary *settingsDict = [NSDictionary dictionaryWithContentsOfFile:finalPath];NSArray *prefSpecifierArray = [settingsDict objectForKey:@"PreferenceSpecifiers"];NSString *firstNameDefault;NSString *lastNameDefault;NSNumber *nameColorDefault;NSNumber *backgroundColorDefault;NSDictionary *prefItem;for (prefItem in prefSpecifierArray){NSString *keyValueStr = [prefItem objectForKey:@"Key"];id defaultValue = [prefItem objectForKey:@"DefaultValue"];if ([keyValueStr isEqualToString:kFirstNameKey]){firstNameDefault = defaultValue;}else if ([keyValueStr isEqualToString:kLastNameKey]){lastNameDefault = defaultValue;}else if ([keyValueStr isEqualToString:kNameColorKey]){nameColorDefault = defaultValue;}else if ([keyValueStr isEqualToString:kBackgroundColorKey]){backgroundColorDefault = defaultValue;}}// since no default values have been set (i.e. no preferences file created), create it hereNSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:firstNameDefault, kFirstNameKey,lastNameDefault, kLastNameKey,nameColorDefault, kNameColorKey,backgroundColorDefault, kBackgroundColorKey, nil];[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];[[NSUserDefaults standardUserDefaults] synchronize];}// we're ready to do, so lastly set the key preference valuesfirstName = [[NSUserDefaults standardUserDefaults] stringForKey:kFirstNameKey];lastName = [[NSUserDefaults standardUserDefaults] stringForKey:kLastNameKey];textColor = [[NSUserDefaults standardUserDefaults] integerForKey:kNameColorKey];backgroundColor = [[NSUserDefaults standardUserDefaults] integerForKey:kBackgroundColorKey];}
?