Как добавить словарь в словарь в plist? - PullRequest
0 голосов
/ 19 апреля 2011

Мне нужно добавить более одного словаря в список.Как это сделать?

NSMutableDictionary *rootDict = [[[NSMutableDictionary alloc] init] autorelease];
NSMutableDictionary *rowsDict = [[[NSMutableDictionary alloc] init] autorelease];
NSMutableDictionary *itemZeroDict1 = [[[NSMutableDictionary alloc] init] autorelease]; 

Мне нужно добавить rowsDict и itemZeroDict1 к rootDict.

Ответы [ 2 ]

0 голосов
/ 20 сентября 2017

Используйте приведенный ниже код для записи словаря в файл .plist.

    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];
0 голосов
/ 19 апреля 2011
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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...