开发者

Is it possible to read the structure of plist file?

I know that NSUserDefaults can read the key and value from plist file. Is it possible to read the structure of a key?

For example:

Key 'Count' is an integer and has option 1,2,3,4,5

the codes below can get the value 'Count'

NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];

if([defaults objectForKey:@"Count"]!=nil)
{

    NSString *s=[[NSString alloc] initWithString: [defaults objectForKey:@"Count"]];
    NSInteger v=[s intValue];
    [s release];
}

But I prefer to get all options and store to a NSArray or开发者_如何学Go a better storage structure.

Is it Possible?


I read bundle settings the following way:

// Get path to Root.plist file in settings bundle and retrieving its contents
NSString* tPath = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"];
settingsBundle = [[NSBundle bundleWithPath:tPath] retain];
NSDictionary* tSetDict = [NSDictionary dictionaryWithContentsOfFile:[tPath stringByAppendingPathComponent:@"Root.plist"]];

// Get array of preference dictionaries 
NSArray* prefs = [tSetDict objectForKey:@"PreferenceSpecifiers"];

// Iterate through dictionaries to find required value
for (NSDictionary* setDict in prefs){
    NSString* type = [setDict objectForKey:@"Type"];
    if (![type isEqualToString:@"PSMultiValueSpecifier"]){
        // Get possible preference values for PSMultiValueSpecifier case
        // You may need to know value type in advance - not sure about that
        NSArray* values = [setDict objectForKey:@"Values"];
    }
}


I'm not exactly sure I understand your question but you can't directly access the user defaults system save through its defined methods. You can't read them out in one big chunk.

The defaults system isn't actually a means of reading plist files, its an more of an API for accessing a database maintained by the OS itself. Although, you don't see it much on the iPhone, its actually a very large and complex system for managing preferences not only for individual apps but also for users, groups of users, computers and networks. It seems trivial on iOS because you don't have the flexibility of configuration and use that you have on MacOS proper.

It would be impossible to read out the entire defaults because they are huge and much larger than you would expect even on iOS.

Instead of jumping through the hoops in the code you showed in the answer, you should access the data in the key using one of the dedicated methods for the type of data stored. In this case:

NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
NSInteger v=[defaults integerForKey:@"Count"];

Even if you did read the defaults out in one chunk, you would just find yourself using the same type of calls and code to access the data in the alternate data structure as you would in using the defaults in the first place. You might as well use the defaults system.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜