2015-12-16 127 views
-2

我有一個代碼來獲取plist文件的數據和將數據寫入plist文件的代碼。現在的情況是,它將加速數據添加到一個數組中(請參閱圖像)。 我想在不同的陣列(見綠圖像)將NSArray寫入plist文件

這裏是我的代碼:

- (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

這真的很難理解 - 你可以嘗試在這裏以純文本的方式陳述,你正在嘗試通過讀取和寫入數組來完成什麼? – RyanR

回答

0

你的問題和解決問題的困惑。也許這將幫助:

你的代碼讀取當前的陣列(NummerTexts),變平,該數組到一個字符串(NummerTextsR),得到了第二個字符串(CurrentVideoNummer),然後建立一個兩元素數組(newArray)。

你的問題似乎是爲什麼你會得到一個兩元素的數組... ...?

如果你不想你的扁平化原始數組不這樣做,只是做的是一個可變的副本,喜歡的東西:

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

添加新的元素:

[existingTexts addObject:currentVideoNummer]; 

然後像你一樣寫回來。

HTH

BTW不要用大寫字母開頭的局部變量名,這就違背了約定,這就是爲什麼語法在你的問題突出是完全錯誤的。

+0

所以我試了一下,這幾乎是我想要的,但現在看起來像這樣:https://www.dropbox.com/s/3a3pxbof9irqkjf/NOT%20YET.png?dl=0 我需要這個:https: //www.dropbox.com/s/avdt33umncb6a7c/5%20%28GREEN%29.png?dl=0 但是,感謝您的幫助。也許你可以幫我 –

+0

從你的圖像,而不是添加一個元素到數組中,你已經創建了一個由兩個元素組成的新數組,其中一個是現有數組,另一個是元素。您需要仔細閱讀代碼並弄清楚您是如何犯這個錯誤的。你也可以在調試器中一步一步看看你的變量是如何變化的,以幫助你理解它。 – CRD

+0

我想出了我的問題,謝謝你的幫助! –