NSUserDefaults 设置捆绑 Plist
我有一个应用程序,它在从 appDelegate 的 appDidFinishLoading 方法加载时检查文件,使用我应该存储在 NSUserDefaults 设置根 Plist 中的 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 模拟器上不起作用?
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];
with some statics:
NSString *kFirstNameKey = @"url";
NSString *kSecondNameKey = @"Web DataSource";
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.
Here is the plist:
Or is it that the settings bundle doesn't work on the Xcode simulator?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要明确保存它。例如,如果 strFirstName 是用户修改的值,那么您可以通过以下方式将其保存到应用程序设置中
You'll need to explicitly save it. For example, if strFirstName is the value that the user modified, then you would save it to the app settings by
事实证明我错过了这一行:
但这只读取每次启动的默认值。它不会保存用户在应用程序设置中修改的值。为什么这个值没有被保存?
Turns out I was missing this line:
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?