pList writeToFile 字典数组 - 仅写入最后一个数组条目

Posted

技术标签:

【中文标题】pList writeToFile 字典数组 - 仅写入最后一个数组条目【英文标题】:pList writeToFile array of dictionaries - only last array entry written 【发布时间】:2014-07-26 21:46:31 【问题描述】:

我是 ios 开发新手,无法将字典对象数组写入文件。

这是我的代码......以及我认为它应该在下面做什么:

    // Step 1.  Copy the plist in the App bundle and create one in the Documents directory

NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Devices.plist"];

NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath: path])

    NSString *bundle = [[NSBundle mainBundle] pathForResource:@"Devices" ofType:@"plist"];

    [fileManager copyItemAtPath:bundle toPath: path error:&error];




// Step 2). Read my plist into a number of internal arrays.

NSArray *rootArray = [NSArray arrayWithContentsOfFile:path];

nameArray           = [[NSMutableArray alloc] init];
MACArray            = [[NSMutableArray alloc] init];
imageArray          = [[NSMutableArray alloc] init];
blockArray          = [[NSMutableArray alloc] init];
synchronisedArray   = [[NSMutableArray alloc] init];


for (NSDictionary *itemData in rootArray)

    [nameArray addObject:[itemData objectForKey:@"name"]];
    [MACArray addObject:[itemData objectForKey:@"MAC"]];
    [imageArray addObject:[itemData objectForKey:@"image"]];
    [blockArray addObject:[itemData objectForKey:@"block"]];
    [synchronisedArray addObject:[itemData objectForKey:@"synchronised"]];




// Step 3. Load my internal arrays into an array of dictionaries.

NSMutableDictionary *outData = [[NSMutableDictionary alloc] init];
NSMutableArray *outArray = [[NSMutableArray alloc] init];

for (int i = 0; i<[nameArray count]; i++)

    [outData removeAllObjects];

    [outData setValue:[nameArray objectAtIndex:i]         forKey:@"name"];
    [outData setValue:[MACArray objectAtIndex:i]          forKey:@"MAC"];
    [outData setValue:[imageArray objectAtIndex:i]        forKey:@"image"];
    [outData setValue:[blockArray objectAtIndex:i]        forKey:@"block"];
    [outData setValue:[synchronisedArray objectAtIndex:i] forKey:@"synchronised"];


    [outArray addObject: outData];

    NSLog(@"Last object in outArray at %i is %@", i, [outArray objectAtIndex:i]);
    NSLog(@"---------------------------");



// Step 4). Write the array back out to the plist.

BOOL result = [outArray writeToFile: path atomically:YES];

if (result)
    NSLog(@"Result YES");
else
    NSLog(@"Result NO");

解释:

1)。从 App 包中读取 plist,然后将其直接复制到 Documents(以便我以后可以写入)。

2)。读入新创建的 plist,并将其加载到多个 NSMutableArray 对象中。

...这里是主程序代码(本例省略)。

3)。读取内部数组并创建 NSDictionary 对象,将每个对象添加到 NSMutableArray (outArray)。

4)。将 outArray 写入 plist。

这一切都很好,除了最后一步。当我查看 plist 时,按预期有 3 个条目......但是......它们都是一样的。它们是 outArray 中最后一个条目的副本。

这是 NSLog 输出:

2014-07-27 09:19:29.533 Filter[7143:60b] Last object in outArray at 0 is 
    MAC = "00:26:B0:CF:48:32";
    block = 0;
    image = "laptop-50";
    name = Laptop;
    synchronised = 1;

2014-07-27 09:19:29.537 Filter[7143:60b] ---------------------------
2014-07-27 09:19:29.539 Filter[7143:60b] Last object in outArray at 1 is 
    MAC = "00:26:B0:CF:48:32";
    block = 0;
    image = "joystick-50";
    name = PS3;
    synchronised = 0;

2014-07-27 09:19:29.540 Filter[7143:60b] ---------------------------
2014-07-27 09:19:29.541 Filter[7143:60b] Last object in outArray at 2 is 
    MAC = "B8:FF:61:09:BC:EE";
    block = 1;
    image = "ipad_copyrighted-50";
    name = "Logan's iPad";
    synchronised = 1;

2014-07-27 09:19:29.542 Filter[7143:60b] ---------------------------
2014-07-27 09:19:29.593 Filter[7143:60b] Result YES

感谢任何帮助/建议...谢谢。

【问题讨论】:

【参考方案1】:

在您编写现有密钥时,新值会覆盖您的现有密钥。用此代码替换上述代码

// Step 3. Load my internal arrays into an array of dictionaries.


NSMutableArray *outArray = [[NSMutableArray alloc] init];

    for (int i = 0; i<[nameArray count]; i++)
    
         NSMutableDictionary *outData = [[NSMutableDictionary alloc] init]; //add this line here

        [outData setValue:[nameArray objectAtIndex:i]         forKey:@"name"];
        [outData setValue:[MACArray objectAtIndex:i]          forKey:@"MAC"];
        [outData setValue:[imageArray objectAtIndex:i]        forKey:@"image"];
        [outData setValue:[blockArray objectAtIndex:i]        forKey:@"block"];
        [outData setValue:[synchronisedArray objectAtIndex:i] forKey:@"synchronised"];


        [outArray addObject: outData];

        NSLog(@"Last object in outArray at %i is %@", i, [outArray objectAtIndex:i]);
        NSLog(@"---------------------------");
    

【讨论】:

非常感谢...解决了问题。我忘记了我的 outArray 实际上只是一个指针列表......而且我的所有条目都指向同一个东西!

以上是关于pList writeToFile 字典数组 - 仅写入最后一个数组条目的主要内容,如果未能解决你的问题,请参考以下文章

writeToFile 不适用于 NSDictionary [重复]

增量写入 plist 文件

writeToFile:atomically: 不保存新的 plist 数据

iphone - writeToFile 没有将新条目保存到 plist

将字典写入文件

iPhone:如何编写字典对象的 plist 数组