-2

I have a code to get the data of a plist file and a code to write data to a plist file. Now the things is, that it add's the readed data in one array (See images). I want it in different arrays (See green image)

HERE IS MY CODE:

- (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];
}

Click link for images

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

1
  • 1
    This is really hard to understand - can you try stating, in plain text here, what it is you are trying to accomplish with the reading and writing of the arrays?
    – RyanR
    Dec 16, 2015 at 1:54

1 Answer 1

0

Your question and problem are confusing. Maybe this will help:

Your code reads your current array (NummerTexts), flattens that array to a single string (NummerTextsR), gets a second string (CurrentVideoNummer), then builds a two-element array (newArray).

Your question appears to be why do you get a two-element array...?

If you don't want to flatten your original array don't do so, just make a mutable copy of it, something like:

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

add your new element:

[existingTexts addObject:currentVideoNummer];

and write it back like you already are.

HTH

BTW Do not start local variable names with an uppercase letter, this goes against convention and is why the syntax highlighting in your question is all wrong.

2
  • So I tried it and it's almost what I want, but it now looks like this: dropbox.com/s/3a3pxbof9irqkjf/NOT%20YET.png?dl=0 I need this: dropbox.com/s/avdt33umncb6a7c/5%20%28GREEN%29.png?dl=0 But thanks for your help already. Maybe you can help me Dec 18, 2015 at 22:29
  • From your images instead of adding an element to an array you have created a new array of two elements, one being the existing array and the other the element. You need to read through your code carefully and figure out how you made this mistake. You can also step through in the debugger and watch how your variables change to help you figure it out.
    – CRD
    Dec 18, 2015 at 23:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.