tags:

views:

50

answers:

5

Hi,

I want to find out programatically whats the root element of the plist,i.e whether it is Array or Dictionary.How can i do this. Can anybody help me with this.

-- Regards,

U'suf

+2  A: 

Load the plist with +[NSPropertyListSerialization propertyListFromData:…], then check the -class of the resulting object.

KennyTM
A: 

Here’s the Core Foundation way:

if (CFGetTypeID((CFPropertyListRef)myPropertyList) == CFDictionaryGetTypeID()) {
    // its a dictionary
}
Nikolai Ruhe
Hi Nikolai,Thank you very much, this was what i was looking for, as i am calling two functions named getArray and getDictionary which are doing propertyListSerialization, but before calling these function i needed to enquire whether it is Array or Dictionary. Thanks again.
A: 

You should not use the class method for this. Use NSObject's isKindOfClass: (or isMemberOfClass:) to test if the object's class is [NSArray class] or [NSDictionary class].

see: +[NSObject class]

Taum
A: 

Hi all,

Thank you so much for your answers.

-- Regards,

U'suf

Please tell me you're at least going to accept one.
Onion-Knight
A: 

Try the below:

NSData *plistData;  
NSString *error;  
NSPropertyListFormat format;  
id plist;  

NSString *localizedPath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"plist"];  
plistData = [NSData dataWithContentsOfFile:localizedPath];   

plist = [NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error];
if([plist isKindOfClass:[NSDictionary class]]){
     //do some ...
}
if([plist isKindOfClass:[NSArray class]]){
     //do some ...
}

Additional reading from Apple.

Iggy