【问题标题】:Need help creating .plist in app需要帮助在应用程序中创建 .plist
【发布时间】:2011-07-22 00:09:26
【问题描述】:

我的应用附带一个如下所示的 .plist:

我希望用户能够添加自定义练习名称。

所以我需要在用户的文件夹中创建一个模仿这种格式的新 .plist。谁能帮我解决这个问题?

我需要这样的东西(伪代码)

if (userData == nil)
{
     then create the .plist file;
     setup the .plist to mimic the format of the img above.
} 
now save exerciseName appropriately.

更新:

if (exerciseArray == nil)
{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"data" ofType:@"plist"];
    NSMutableArray *rootLevel = [[NSMutableArray alloc]initWithContentsOfFile:path];
    self.exerciseArray = rootLevel;
    [rootLevel release];
}

【问题讨论】:

标签: iphone objective-c nsarray plist


【解决方案1】:

您要做的是将 Plist 加载到 NSDictionary 中,然后将该 NSDictionary 编码回应用程序文件夹中的 Plist 文件。在你的 applicationDidFinishLoading: 方法中,我会做这样的事情:

NSString * documentFile = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/myPlist.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:documentFile]) {
    // create a copy of our resource
    NSString * resPath = [[NSBundle mainBundle] pathForResource:@"myPlist" ofType:@"plist"];
    // NOTE: replace @"myPlist" with the name of the file in your Resources folder.
    NSDictionary * dictionary = [[NSDictionary alloc] initWithContentsOfFile:resPath];
    [dictionary writeToFile:documentFile atomically:YES];
    [dictionary release];
}

然后,当你想添加一个项目时,你想使用一个 NSMutableDictionary 来修改和保存应用程序的文档目录中的现有 plist:

- (void)addExercise {
    NSMutableDictionary * changeMe = [[NSMutableDictionary alloc] initWithContentsOfFile:documentFile];
    ... make changes ...
    [changeMe writeToFile:documentFile atomically:YES];
    [changeMe release];
}

要进行更改,您需要找到包含练习数组的子词典。然后使用 NSMutableDictionary 上的 setObject:forKey: 方法设置一个包含新练习列表的新数组。这可能看起来像这样:

NSMutableArray * list = [NSMutableArray arrayWithArray:[changeMe objectForKey:@"list"]];
NSArray * exercises = [[list objectAtIndex:10] objectForKey:@"exercises"];
NSDictionary * newExercise = [NSDictionary dictionaryWithObject:@"Type a LOT" forKey:@"exerciseName"];
exercises = [exercises arrayByAddingObject:newExercise];
NSMutableDictionary * dict = [NSMutableDictionary dictionaryWithDictionary:[list objectAtIndex:10]];
[dict setObject:exercises forKey:@"exercises"];
[list replaceObjectAtIndex:10 withObject:dict];
[changeMe setObject:list forKey:@"list"];

一旦您做出更改,请务必记住将changeMe 写入文档目录中的 plist 文件。

【讨论】:

  • 实际上,我想将 2 个 .plist 分开。我已经将原始数组编码到viewDidLoad 中的数组/字典中,我想对自定义数组执行相同的操作,然后在需要加载表格时合并数组。
【解决方案2】:

读取和写入属性列表的最简单方法是使用 NSArray 或 NSDictionary 类。您的屏幕截图似乎是顶层的数组,因此我将在示例中使用该假设。

首先您需要用户文件和原始文件的路径。

NSString *pathToUserFile; // Get a path to the file in the documents directory
NSString *pathToDefaultFile; // Get a path to the original file in the application bundle

然后您尝试加载

NSMutableArray *userData;
NSArray *temporary = [NSArray arrayWithContentsOfFile:pathToUserFile];
if(!temporary) temporary = [NSArray arrayWithContentsOfFile:pathToDefaultFile];

由于您使用的是多层容器,我假设您需要最里面的数组和字典是可变的。 NSMutableArray 的正常初始化不会这样做,因此您需要使用 CFPropertyListCreateDeepCopy 并设置具有可变容器的选项。

userData = (NSMutableArray *)CFPropertyListCreateDeepCopy(NULL,(CFArrayRef)temporary,kCFPropertyListMutableContainers);

您现在有一个可变对象来表示您的数据。您可以像处理任何数组一样添加对象或修改现有对象,但您只能添加字符串、数字、数据对象、字典、数组和日期,因为这些是属性列表中唯一有效的类型。

[userData addObject:newDataObject];

最后,您将数据写入到文档目录中的文件中。 writeToFile:atomically: 方法将尝试写出一个属性列表,如果成功则返回 YES。如果无法写入文件,或者内容不是所有有效的属性列表对象,它将失败并返回 NO。

if(![userData writeToFile:pathToUserFile atomically:YES]) {
    NSLog(@"Error writing to file");
}

【讨论】:

  • 谢谢,让我试试这个。我还添加了用于将原始 plist 中的 exerciseName 数据加载到我的数组中的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-23
  • 2013-02-15
  • 2013-01-19
  • 2017-02-02
  • 1970-01-01
相关资源
最近更新 更多