Iphone 需要帮助在应用程序中创建.plist
我的应用附带一个.plist,如下所示:Iphone 需要帮助在应用程序中创建.plist,iphone,objective-c,nsarray,plist,Iphone,Objective C,Nsarray,Plist,我的应用附带一个.plist,如下所示: NSString * documentFile = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/myPlist.plist"]; if (![[NSFileManager defaultManager] fileExistsAtPath:documentFile]) { // create a copy of our resource NSString * resPath
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];
}
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"];
我希望用户能够添加自定义的exerciseName
所以我需要在用户的文档文件夹中创建一个新的.plist,它模仿这种格式。有人能帮我吗
我需要这样的东西(伪代码)
更新:
if (exerciseArray == nil)
{
NSString *path = [[NSBundle mainBundle]pathForResource:@"data" ofType:@"plist"];
NSMutableArray *rootLevel = [[NSMutableArray alloc]initWithContentsOfFile:path];
self.exerciseArray = rootLevel;
[rootLevel release];
}
读取和写入属性列表的最简单方法是使用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];
最后,将数据写入documents目录中的文件。writeToFile:atomically:
方法将尝试写出属性列表,如果成功,则返回YES。如果文件无法写入,或者内容不是所有有效的属性列表对象,则它将失败并返回NO
if(![userData writeToFile:pathToUserFile atomically:YES]) {
NSLog(@"Error writing to file");
}
您要做的是将Plist加载到NSDictionary中,并将该NSDictionary编码回应用程序文档文件夹中的Plist文件。在ApplicationIDFinishLoading:方法中,我将执行以下操作:
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];
}
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"];
然后,当您要添加项目时,您需要使用NSMutableDictionary修改并保存应用程序文档目录中的现有plist:
- (void)addExercise {
NSMutableDictionary * changeMe = [[NSMutableDictionary alloc] initWithContentsOfFile:documentFile];
... make changes ...
[changeMe writeToFile:documentFile atomically:YES];
[changeMe release];
}
要进行更改,您需要找到包含练习数组的子字典。然后使用NSMutableDictionary上的setObject:forKey:方法设置包含新练习列表的新数组。这可能看起来像这样:
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];
}
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
写入documents目录中的plist文件。实际上,我想将2.plists分开。我已经在viewdiload
中将原始的一个编码到数组/字典中,我想对自定义的一个进行同样的编码,然后在需要加载表时合并数组。另请参见苹果的,特别感谢,让我试试这个。我还添加了用于将exerciseName数据从原始plist加载到数组中的代码。