0

我正在以编程方式创建一个直接指向文档目录的 plist 文件。这是我的代码..

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"myList.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath: path])
{
    path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @"myList.plist"] ];
}
NSMutableArray *data ;
if ([fileManager fileExistsAtPath: path])
{
    data = [[NSMutableArray alloc] initWithContentsOfFile: path];

}
else
{
    // If the file doesn’t exist, create an empty dictionary
    data = [[NSMutableArray alloc] init];
}
data = [[NSMutableArray  alloc] initWithCapacity:5];
[data addObject:value1];
[data addObject:value2];
[data addObject:value3];

它使用这种格式创建 plist 文件..

<array>
        <string>1</string>
        <string>2</string>
        <string>3</string>
</array>

现在,我想向它添加另一个索引,例如,

<array>
        <string>1</string>
        <string>2</string>
        <string>3</string>
</array>
<array>
        <string>11</string>
        <string>22</string>
        <string>33</string>
</array>  

如何以编程方式在我的 plist 文件中添加索引?
任何帮助或建议将不胜感激!

谢谢

4

2 回答 2

4

尝试

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"myList.plist"];

NSMutableArray *data = [[NSMutableArray alloc] initWithContentsOfFile: path];
if (!data) {
    data = [NSMutableArray array];
}

[data addObject:@[value1,value2,value3]];

[data writeToFile:path atomically:YES];
于 2013-04-25T12:51:33.783 回答
1

您应该将您的 plist 组织为字典。对应于每个键,您可以存储数组对象。这很容易。将您的数组存储到 aNSDictionary中,并使用适当的键作为索引号。然后把字典放到 plist 上

<dict>
  <key>0</key>
  <array>
      <string>1</string>
      <string>2</string>
      <string>3</string>
  </array>
  <key>1</key>
  <array>
      <string>11</string>
      <string>22</string>
      <string>33</string>
  </array>
</dict>
于 2013-04-25T12:43:00.373 回答