How to add dictionary to dictionary in plist?
I need to add more than one dictionary to a plist. How can do it?
NSMutableDictionary *rootDict = [[[NSMutableDictionary alloc] init] autorelease];
NSMutableDictionary *rowsDict = [[[NSMutableDictionary alloc] init] autorelease];
NSMutableDictionary *itemZeroDict1 = [[[NSMutableDictionary alloc] init] autorelease];
I need to add rowsDict
and itemZeroDict1
to roo开发者_运维问答tDict
.
NSDictionary *root = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSDictionary dictionaryWithObject:@"1" forKey:@"number"],
[NSDictionary dictionaryWithObject:@"2" forKey:@"number"],
nil];
NSString *error = nil;
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:root
format:NSPropertyListBinaryFormat_v1_0
errorDescription:&error];
if (plistData == nil)
NSLog(@"Error serializing plist: %@", error);
else
// Do whatever you want with plistData
[error release]; // this is an exception to the usual memory management in Cocoa
Use below code to write dictionary to .plist file.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *plistPath = [[paths lastObject] stringByAppendingPathComponent:@"friendList.plist"];
/* Code to remove Existing file from Document directory */
if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]){
[[NSFileManager defaultManager] removeItemAtPath:plistPath error:nil];
}
////////////////
/** Copy Plist file from your bundle resource to Document directory */
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"friendList" ofType:@"plist"];
BOOL sucesss = [[NSFileManager defaultManager] copyItemAtPath:bundle toPath:plistPath error:nil];
if (!sucesss) {
}
NSMutableArray *arryDict = [[NSMutableArray alloc] init];
for (NSDictionary *dict in arryOfDictionary) {
NSDictionary *dictFriend = @{@"key":@"value"};
[arryDict addObject:dictFriend];
}
[arryDict writeToFile:plistPath atomically:NO];
精彩评论