我是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
感谢任何帮助/建议......谢谢。
答案 0 :(得分:0)
在您编写时,您的现有密钥将被新值覆盖。请使用此
替换上述代码// 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(@"---------------------------");
}