2012-10-04 122 views
2

我使用xCode 3.2,然後移動到xCode 4.2並從Settings.bundle獲取一些值...它工作正常。iphone:Settings.bundle返回空值

雖然我需要在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' 

之後,我可以看到Root.plist但現在在應用程序無法得到它的值。實際上得到而不是價值。

下面是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); 
} 

enter image description here

回答

4

我得到了解決,只是調用下面的代碼在applicationDidFinishLaunchingWithOptions初始化用戶默認值。並且它可以工作

將第一行中的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]; 
} 
+0

與XCode 6.3相同的問題,但您的解決方案仍在工作。 – szpetip