将数据从plist获取到NSMutableArray并将NSMutableArray写入相同的plist iPhone [英] getting data from plist to NSMutableArray and writing the NSMutableArray to same plist iPhone

查看:132
本文介绍了将数据从plist获取到NSMutableArray并将NSMutableArray写入相同的plist iPhone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using this code I am trying to get the data from Templates.plist file but I am getting null value in array.

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

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

这是我的plist文件:

This is my plist file:

另外我想知道怎么样我在这个plist文件中添加了更多的字符串,并从这个plist中删除了一个字符串。

Also I want to know how can I add more strings to this plist file and delete a string from this plist.

推荐答案

首先你不能写任何东西在你身上mainBundle。为了给你写信,你需要将它复制到文件目录。这样做是这样的:

First off you cannot write to anything in you mainBundle. For you to write to you plist you need to copy it to the documents directory. This is done like so:

- (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]);
}

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

So calling this function will check if the file exists in the documents directory. If it doesn't it copies the file to the documents directory. If the file exists it just returns. Next you just need to access the file to be able to read and write to it. To access you just need the path to the documents directory and to add your file name as a path component.

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

从plist获取数据:

To get the data from the plist:

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

将文件写回文件目录。

    [array writeToFile:filePath atomically: YES];

这篇关于将数据从plist获取到NSMutableArray并将NSMutableArray写入相同的plist iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆