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);
}
}

Discover more from mycodetips

Subscribe to get the latest posts sent to your email.

Discover more from mycodetips

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

Continue reading

Scroll to Top