0

使用此代码,我试图从 Templates.plist 文件中获取数据,但我在数组中得到空值。

//from plist
NSString *path = [[NSBundle mainBundle] pathForResource:@"Templates" ofType:@"plist"];
NSMutableArray *arry=[[NSMutableArray alloc]initWithContentsOfFile:path];

NSLog(@"arrayArray:::: %@", arry);

这是我的 plist 文件:

列表文件

另外我想知道如何向这个 plist 文件添加更多字符串并从这个 plist 中删除一个字符串。

4

3 回答 3

3

首先,您不能写入 mainBundle 中的任何内容。为了让您写信给您的 plist,您需要将其复制到文档目录。这样做是这样的:

- (void)createEditableCopyOfIfNeeded 
{
     // First, test for existence.
     BOOL success;

     NSFileManager *fileManager = [NSFileManager defaultManager];
     NSError *error;
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *documentsDirectory = [paths objectAtIndex:0];
     NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:@"Template.plist"];
     success = [fileManager fileExistsAtPath:writablePath];

     if (success) 
         return;

     // The writable file does not exist, so copy from the bundle to the appropriate location.
     NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Template.plist"];
     success = [fileManager copyItemAtPath:defaultPath toPath:writablePath error:&error];
     if (!success) 
         NSAssert1(0, @"Failed to create writable file with message '%@'.", [error localizedDescription]);
}

因此调用此函数将检查文件是否存在于文档目录中。如果不是,它将文件复制到文档目录。如果文件存在,它只会返回。接下来,您只需要访问该文件即可对其进行读写。要访问您只需要文档目录的路径并将您的文件名添加为路径组件。

NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [docDir stringByAppendingPathComponent:@"Template.plist"];

要从 plist 中获取数据:

NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:filePath];

将文件写回文档目录。

    [array writeToFile:filePath atomically: YES];
于 2012-02-08T17:03:57.520 回答
2
-(NSMutableArray *)start1

 {

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"Templates.plist"];



if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]){

    plistArr = [NSMutableArray arrayWithContentsOfFile: plistPath];

}
else {
    plistArr = [[NSMutableArray alloc] init];

}
return plistArr;
}


- (void)createNewRecordWithName:(NSMutableDictionary *)dict

 {

plistArr=[self start1];
[plistArr addObject: dict];
//[dict release];
[self writeProductsToFile:plistArr];

 }

  - (void)writeProductsToFile:(NSMutableArray *)array1 {

   NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"Template.plist"];

[array1 writeToFile:plistPath atomically:YES];

   }
于 2012-02-08T12:36:15.193 回答
0

要将 plist 放入可变数组,请使用此类方法:

NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:path];

然后,从数组中添加/删除字符串。要将数组保存回 plist,请使用:

[array writeToFile:path atomically:YES];
于 2012-02-08T15:05:51.443 回答