NSUserDefaults设置捆绑包列表 [英] NSUserDefaults Settings Bundle Plist
问题描述
我有一个应用程序,它使用我应该存储在NSUserDefaults设置根Plist中的url值,从appDelegate的appDidFinishLoading方法中加载文件时检查文件:
I have an app that checks for a file upon load from the appDelegate's appDidFinishLoading method using a url value that I supposedly store in the NSUserDefaults Settings Root Plist:
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.
When I NSLog the nsUserDefURL, i still get the default value in the settings plist. I went into the settings for the app (didn't know i could do so in the simulator) and I modified the value in the url field, but i STILL get the test2.xml value that is the default placeholder value in the settings root plist.
这里是plist:
还是设置捆绑包在Xcode模拟器上不起作用?
Or is it that the settings bundle doesn't work on the Xcode simulator?
推荐答案
结果是我错过了这一行:
Turns out I was missing this line:
nsUserDefURL = [[NSUserDefaults standardUserDefaults] stringForKey:kFirstNameKey];
但这只会在每次启动时读取默认值.它不会在应用程序设置中保存用户修改的值.为什么没有保存该值?
But this only reads the default value each launch. It won't save the user modified value in the app settings. Why is that value not being saved?
这篇关于NSUserDefaults设置捆绑包列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!