NSUserDefaults 设置捆绑 Plist

发布于 2024-12-25 12:42:38 字数 1696 浏览 2 评论 0原文

我有一个应用程序,它在从 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:Root.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:Root.Plist

Or is it that the settings bundle doesn't work on the Xcode simulator?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾听心声的旋律 2025-01-01 12:42:38

您需要明确保存它。例如,如果 strFirstName 是用户修改的值,那么您可以通过以下方式将其保存到应用程序设置中

 NSUserDefaults* ud = [NSUserDefaults standardUserDefaults];
 [ud setObject:strFirstName forKey:kFirstNameKey];
 [ud sychronize];

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

 NSUserDefaults* ud = [NSUserDefaults standardUserDefaults];
 [ud setObject:strFirstName forKey:kFirstNameKey];
 [ud sychronize];
娇妻 2025-01-01 12:42:38

事实证明我错过了这一行:

nsUserDefURL = [[NSUserDefaults standardUserDefaults] stringForKey:kFirstNameKey];  

但这只读取每次启动的默认值。它不会保存用户在应用程序设置中修改的值。为什么这个值没有被保存?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文