简体   繁体   English

iPhone:Settings.bundle返回空值

[英]iphone : Settings.bundle returns null value

I was using xCode 3.2 and then moved to xCode 4.2 and getting some values from Settings.bundle ... it was working fine. 我使用的是xCode 3.2,然后移至xCode 4.2,并从Settings.bundle中获取了一些值。

Mean while I need to edit some values in Settings.bundle but The Root.plist file was not showing so I follow the below procedure but did not make any change in file. 这意味着虽然我需要在Settings.bundle中编辑一些值,但是Root.plist文件没有显示,所以我按照以下过程进行操作,但未对文件进行任何更改。

1) Click on the Settings.Bundle file, go over to the utilities window,
and look in the File Inspector.
2) Using the drop-down, change the file type to 'Application Bundle'

After that I could see Root.plist but now could not get its values in application. 之后,我可以看到Root.plist,但现在无法在应用程序中获取其值。 Actually getting Null instead of value. 实际上得到的是Null而不是价值。

Below is code and image of Settings.bundle 以下是Settings.bundle的代码和图像

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
host = [defaults stringForKey:@"meter_preference"];
if(host == nil)
{
    host = @"10.20.20.1";
    DDLogError(@"Meter host is nil from NSUserDefaults, defaulting to %@", host);
}

在此处输入图片说明

I got the solution, just call the below code in applicationDidFinishLaunchingWithOptions to initialize User defaults. 我得到了解决方案,只需在applicationDidFinishLaunchingWithOptions中调用以下代码即可初始化用户默认设置。 and it works 它有效

Replace somePropertyYouExpect in first line with property you stored in User Defaults. 用存储在用户默认值中的属性替换第一行中的somePropertyYouExpect

if (![[NSUserDefaults standardUserDefaults] objectForKey:@"somePropertyYouExpect"])  {

    NSString  *mainBundlePath = [[NSBundle mainBundle] bundlePath];
    NSString  *settingsPropertyListPath = [mainBundlePath
                                           stringByAppendingPathComponent:@"Settings.bundle/Root.plist"];

    NSDictionary *settingsPropertyList = [NSDictionary 
                                          dictionaryWithContentsOfFile:settingsPropertyListPath];

    NSMutableArray      *preferenceArray = [settingsPropertyList objectForKey:@"PreferenceSpecifiers"];
    NSMutableDictionary *registerableDictionary = [NSMutableDictionary dictionary];

    for (int i = 0; i < [preferenceArray count]; i++)  { 
        NSString  *key = [[preferenceArray objectAtIndex:i] objectForKey:@"Key"];

        if (key)  {
            id  value = [[preferenceArray objectAtIndex:i] objectForKey:@"DefaultValue"];
            [registerableDictionary setObject:value forKey:key];
        }
    }

    [[NSUserDefaults standardUserDefaults] registerDefaults:registerableDictionary]; 
    [[NSUserDefaults standardUserDefaults] synchronize]; 
} 

From your code , try this thinks.. 从您的代码中,尝试这样思考。

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    host = [defaults stringForKey:@"meter_preference"];
    if(!host == nil)
    {
        host = @"10.20.20.1";
        DDLogError(@"Meter host is nil from NSUserDefaults, defaulting to %@", host);
    }

OR 要么

Review this link may be helped you... 查看此链接可能会帮助您...

iPhone - reading Setting.bundle returns wrong values iPhone-阅读Setting.bundle返回错误的值

NSUserDefaults Settings Bundle Plist NSUserDefaults设置捆绑包列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM