将NSArray写入plist文件

时间:2015-12-16 00:22:26

标签: ios objective-c nsarray plist

我有一个代码来获取plist文件的数据和将数据写入plist文件的代码。现在的情况是,它将readed数据添加到一个数组中(参见图像)。 我希望它在不同的阵列中(见绿色图像)

这是我的代码:

- (void)WriteData {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *path = [documentsDirectory stringByAppendingPathComponent:@"MuziekList.plist"];

if ([fileManager fileExistsAtPath:path] == NO) {
    NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"MuziekList" ofType:@"plist"];
    [fileManager copyItemAtPath:resourcePath toPath:path error:&error];
}
//_______________________________________________________________________________________________________________
NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile: path];

//load from savedStock example int value
NSArray *NummerTexts;
NummerTexts = [savedStock objectForKey:@"Nummers"];
NSString *NummerTextsR = [[NummerTexts valueForKey:@"description"] componentsJoinedByString:@" - "];

NSArray *ArtiestTexts;
ArtiestTexts = [savedStock objectForKey:@"Artiesten"];
NSString *ArtiestTextsR = [[ArtiestTexts valueForKey:@"description"] componentsJoinedByString:@" - "];
//_______________________________________________________________________________________________________________

NSUserDefaults *CurrentVideoNummerResult = [NSUserDefaults standardUserDefaults];
NSString *CurrentVideoNummer = [CurrentVideoNummerResult stringForKey:@"CurrentVideoNummer"];

NSUserDefaults *CurrentVideoArtiestResult = [NSUserDefaults standardUserDefaults];
NSString *CurrentVideoArtiest = [CurrentVideoArtiestResult stringForKey:@"CurrentVideoArtiest"];

/*
NSUserDefaults *CurrentVideoIDResult = [NSUserDefaults standardUserDefaults];
NSString *CurrentVideoID = [CurrentVideoIDResult stringForKey:@"CurrentVideoID"];
*/

NSMutableDictionary *plist = [[NSDictionary dictionaryWithContentsOfFile:path] mutableCopy];
NSMutableArray *newArray = [[NSMutableArray alloc] init];
NSMutableArray *newArray2 = [[NSMutableArray alloc] init];

newArray = [NSMutableArray arrayWithObjects:CurrentVideoNummer, NummerTextsR, nil];
[plist setObject:newArray forKey:@"Nummers"];


newArray2 = [NSMutableArray arrayWithObjects:CurrentVideoArtiest, ArtiestTextsR, nil];
[plist setObject:newArray2 forKey:@"Artiesten"];

[plist writeToFile:path atomically:YES];
}

点击图片链接

https://www.dropbox.com/sh/wrk8h8cnwye8myx/AADl4omkGdl3S4ESXv6NbymVa?dl=0

1 个答案:

答案 0 :(得分:0)

您的问题和疑问令人困惑。也许这会有所帮助:

您的代码读取当前数组(NummerTexts),将该数组展平为单个字符串(NummerTextsR),获取第二个字符串(CurrentVideoNummer),然后构建一个双元素数组(newArray)。

你的问题似乎是为什么你得到一个双元素阵列......?

如果您不想展平原始阵列,请不要复制它,例如:

NSMutableArray *existingTexts = [[NummerTexts valueForKey:@"description"] mutableCopy];

添加新元素:

[existingTexts addObject:currentVideoNummer];

并像往常一样把它写回来。

HTH

顺便说一句不要用大写字母启动局部变量名,这违反了惯例,这就是为什么你的问题中的语法突出显示都是错误的。