How to read data from property list file (.plist) to NSDictionary in IOS.

Read data from property list

- (NSDictionary *) dictionaryWithPropertyListAtPath: (NSString *) path {
NSString *error = nil; 
NSPropertyListFormat format; 

NSData *plistXML = [ [NSFileManager defaultManager] contentsAtPath: path];
NSDictionary *result = (NSDictionary *) [NSPropertyListSerialization propertyListFromData: plistXML 
mutabilityOption: NSPropertyListMutableContainersAndLeaves 
format: &format 
errorDescription: &error];
if (error != nil) {
NSLog(@"Error while reading plist: %@", error);
}
return result;
}

- (void) saveDictionary: (NSDictionary *) dictionary toPropertyListAtPath: (NSString *) path {
NSString *error = nil;

NSData *plistData = [NSPropertyListSerialization dataFromPropertyList: dictionary
format: NSPropertyListXMLFormat_v1_0
errorDescription: &error];

if (plistData != nil) {
[plistData writeToFile: path atomically: YES];
} else {
NSLog(@"Error while writing plist: %@", error);
}
}

One Comment

  1. Hi Ranjan,
    Loved your tips. I am in urgent need to hire a person like you for my company. Can you please get in touch with me asap at birju@karmadigi.com?

    thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

Discover more from CODE t!ps

Subscribe now to keep reading and get access to the full archive.

Continue reading