2013-02-11 60 views
0

我見過很多關於添加到plist文件的教程,但我似乎無法正確地將嵌套標籤插入到plist文件中。 我想下面的行添加到我的Info.plist文件如何將以下幾行添加到我的plist文件中?

<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]; 
+1

現在還不清楚你想要添加什麼,取而代之的是什麼。 – 2013-02-11 09:13:05

+1

最新的問題? – 2013-02-11 09:13:15

+0

我編輯了我的問題。再次看到 – zzzzz 2013-02-11 09:14:14

回答

2

需要設置一個字符串數組,作爲鍵@"AppTag"對象,像這樣:

NSArray *strings = @[@"hidden"]; 
plist[@"AppTag"] = strings; 
1

你需要做的是添加stringsarray,然後arraydictionary的文件。在下面的例子中,我加入了一些單詞及其定義,以一個名爲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]; 

我希望這有助於。

另外,我覺得這是不是做工精細的部分:@"/Users/me/Desktop/Info2.plist"

嘗試NSLogging。

+0

如果你看到我的要求。字符串標籤必須嵌套在數組標籤中。我只是想知道我可以如何實現這一目標?我想添加我給定的xml到一個已經存在的info.plist文件,但它非常混亂。告訴我如何實現這個目標? – zzzzz 2013-02-11 09:28:53

+1

你正在做的是做嵌套標籤的權利,但你不能將文件保存在應用程序的'Documents'文件夾之外,所以你不能將文件保存在桌面上。嘗試使用我的代碼的第二部分將其保存在該文件夾中。 – Neeku 2013-02-11 09:37:20

+1

其越獄設備,我可以保存在任何地方我想:) – zzzzz 2013-02-11 09:38:05

1

可以嘗試下面的代碼

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

在viewDidLoad中或任何方法

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

希望這會有所幫助..快樂編碼

相關問題