0

I have seen a lot of tutorials about adding to a plist file but I can't seem to correctly insert nested tags into the plist file. I want to add the following lines to my Info.plist file

<key>AppTag</key> <array>
     <string>hidden</string> </array>


NSMutableDictionary *plist = [[NSDictionary dictionaryWithContentsOfFile:@"/Users/me/Desktop/Info2.plist"] mutableCopy];

  [plist setObject:@"AppTag" forKey:@"key"];
  [plist setObject:@"hidden" forKey:@"string"];

    [plist writeToFile:@"/Users/me/Desktop/Info2.plist" atomically:YES];
    [plist release];
rptwsthi
  • 10,094
  • 10
  • 68
  • 109
zzzzz
  • 1,209
  • 2
  • 18
  • 45
  • 1
    It's unclear what you want to add and what you're getting instead. – Carl Veazey Feb 11 '13 at 09:13
  • I edited my question.See it again – zzzzz Feb 11 '13 at 09:14
  • 1
    are getting some error while executing this code? it seems to be ok, by the way, the file `@"/Users/me/Desktop/Info2.plist"` exists?, if not your `plist` variable will be `null` and them *nothing will happen* – tkanzakic Feb 11 '13 at 09:23
  • I am not getting any error.I am getting the wrong output.and I am not getting nested tags.I just want to know how to add these tags to my plist. – zzzzz Feb 11 '13 at 09:24

3 Answers3

2

You need to set an array of strings as the object for key @"AppTag", like so:

NSArray *strings = @[@"hidden"];
plist[@"AppTag"] = strings;
Carl Veazey
  • 18,392
  • 8
  • 66
  • 81
1

What you need to do is add strings to your array, and then the array to the dictionary of the file. In the following example, I'm adding some words and their definitions to a file called WordList.plist:

NSArray *values = [[NSArray alloc] initWithObjects:word.name, word.definition, nil];
NSArray *keys = [[NSArray alloc] initWithObjects: NAME_KEY, DEFINITION_KEY, nil];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjects:values forKeys:keys];
[wordsListArray addObject:dict];

NSString *destinationPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
 NSUserDomainMask, YES) lastObject];
destinationPath = [destinationPath stringByAppendingPathComponent:@"WordList.plist"];
[wordsListArray writeToFile:destinationPath atomically:YES];

I hope this helps.

Also, I think this is the part that's not working fine: @"/Users/me/Desktop/Info2.plist"

Try NSLogging.

Neeku
  • 3,646
  • 8
  • 33
  • 43
  • If you see my requirements.The string tag must be nested in the array tag.I just wanna know how I can achieve this? I want to add my given xml to an already existing info.plist file but its very confusiong.Tell me how do I achieve that? – zzzzz Feb 11 '13 at 09:28
  • 1
    What you're doing is right for doing the nested tags, but you cannot save a file outside the `Documents` folder of your app, so you can't save the file on Desktop. Try using the second part of my code to save it in that folder. – Neeku Feb 11 '13 at 09:37
  • 1
    its a jailbroken device and I can save anywhere I want :) – zzzzz Feb 11 '13 at 09:38
  • 1
    Hmm... Then try NSLog to see what you get. – Neeku Feb 11 '13 at 09:40
1

You can try following code

-(NSString *) datafilepath{



NSArray *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documents=[path objectAtIndex:0];
return [documents stringByAppendingFormat:@"/Info2.plist"]; // you can change the path to desktop
}

Under ViewDidLoad or any method

NSString *filepath3=[self datafilepath];
if ([[NSFileManager defaultManager]fileExistsAtPath:filepath3]) {
    pricedict=[[NSMutableDictionary alloc]initWithContentsOfFile:filepath3];
}

[pricedict setObject:@"AppTag" forKey:@"key"];
[pricedict setObject:@"hidden" forKey:@"string"];

[pricedict writeToFile:pngfilepath atomically:YES];

Hope this helps.. Happy coding 

Vishnu
  • 2,243
  • 2
  • 21
  • 44