0

plist ファイルへの追加に関する多くのチュートリアルを見てきましたが、ネストされたタグを plist ファイルに正しく挿入できないようです。次の行を Info.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];
4

3 に答える 3

2

@"AppTag"次のように、 key のオブジェクトとして文字列の配列を設定する必要があります。

NSArray *strings = @[@"hidden"];
plist[@"AppTag"] = strings;
于 2013-02-11T09:29:01.310 に答える
1

あなたがする必要があるのはあなたのに、そして次にファイルのに追加stringsすることです。次の例では、いくつかの単語とその定義を次のファイルに追加しています。arrayarraydictionaryWordList.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を試してください。

于 2013-02-11T09:25:23.467 に答える
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];

これがお役に立てば幸いです..ハッピーコーディング</p>

于 2013-02-11T09:46:07.720 に答える