iphone:Settings.bundle返回空值
我使用的是xCode 3.2,然后移动到xCode 4.2,并从Settings.bundle获取一些值。。。它工作得很好 虽然我需要编辑Settings.bundle中的一些值,但是Root.plist文件没有显示,所以我按照以下步骤操作,但没有对文件进行任何更改iphone:Settings.bundle返回空值,iphone,objective-c,ios,xcode,ipad,Iphone,Objective C,Ios,Xcode,Ipad,我使用的是xCode 3.2,然后移动到xCode 4.2,并从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
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,但现在无法在应用程序中获取其值。实际获取的是Null而不是值
下面是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);
}
从您的代码中,尝试以下内容:
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);
}
或
查看此链接可能会对您有所帮助
我得到了解决方案,只需在ApplicationIDFinishLaunchingWithOptions中调用以下代码即可初始化用户默认值。它是有效的 将第一行中的
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];
}
if(![[NSUserDefaults standardUserDefaults]objectForKey:@“somePropertyYouExpect”]){
NSString*mainBundlePath=[[NSBundle mainBundle]bundlePath];
NSString*settingsPropertyListPath=[mainBundlePath]
stringByAppendingPathComponent:@“Settings.bundle/Root.plist”];
NSDictionary*设置属性列表=[NSDictionary]
包含内容的字典文件:settingsPropertyListPath];
NSMutableArray*preferenceArray=[settingsPropertyList objectForKey:@“PreferenceSpecifiers”];
NSMutableDictionary*registerableDictionary=[NSMutableDictionary];
对于(int i=0;i<[preferenceArray count];i++{
NSString*key=[[preferenceArray objectAtIndex:i]objectForKey:@“key”];
如果(关键){
id值=[[preferenceArray objectAtIndex:i]objectForKey:@“DefaultValue”];
[registerableDictionary setObject:值forKey:键];
}
}
[[NSUserDefaults standardUserDefaults]registerDefaults:registerableDictionary];
[[NSUserDefaults standardUserDefaults]同步];
}
XCode 6.3也存在同样的问题,但您的解决方案仍然有效。