我有一个应用程序在appDelegate的appDidFinishLoading方法加载时检查文件,使用我认为存储在NSUserDefaults设置根目录中的url值:
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"];
NSDictionary *prefItem;
for (prefItem in prefSpecifierArray){
NSString *keyValueStr = [prefItem objectForKey:@"Key"];
if ([keyValueStr isEqualToString:kFirstNameKey]){
nsUserDefURL = [prefItem objectForKey:@"DefaultValue"];
}
if ([keyValueStr isEqualToString:kSecondNameKey]){
NSLog(@"You are using local data:%@",[prefItem objectForKey:@"DefaultValue"]);
}
}
NSLog(@" this is the url == %@", nsUserDefURL);
// since no default values have been set (i.e. no preferences file created), create it here
NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:nsUserDefURL,kFirstNameKey,@"YES",kSecondNameKey,nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
[[NSUserDefaults standardUserDefaults] synchronize];
有一些静电:
NSString *kFirstNameKey = @"url";
NSString *kSecondNameKey = @"Web DataSource";
当我NSLog nsUserDefURL时,我仍然在设置plist中获得默认值。我进入了应用程序的设置(不知道我可以在模拟器中这样做)并且我修改了url字段中的值,但我仍然得到test2.xml值,这是设置根目录中的默认占位符值plist中。
这是plist:
或者设置包在Xcode模拟器上不起作用吗?
答案 0 :(得分:1)
您需要明确保存它。例如,如果strFirstName是用户修改的值,那么您可以通过
将其保存到应用程序设置中 NSUserDefaults* ud = [NSUserDefaults standardUserDefaults];
[ud setObject:strFirstName forKey:kFirstNameKey];
[ud sychronize];
答案 1 :(得分:0)
原来我错过了这一行:
nsUserDefURL = [[NSUserDefaults standardUserDefaults] stringForKey:kFirstNameKey];
但是这只会在每次启动时读取默认值。它不会在应用程序设置中保存用户修改的值。为什么没有保存这个值?