如何在plist中存储NSMutableArray

时间:2013-04-22 11:30:40

标签: ios objective-c nsmutablearray plist

我的NSMutableArray名称为“添加”,其自我名称为cell(在UITableView中) 我希望将此“添加”NSMutableArray存储在.plist文件中。

这是“添加”代码:

//NSArray *NaMe;
//NSMutableArray *add;
//NSMutableArray *all;
for (int i =0; i<11; i++) {
        NSIndexPath *indexPath = [self.Table indexPathForSelectedRow];
        NaMe = [[all objectAtIndex:(indexPath.row)+i]objectForKey:@"name"];
        if(!add){
            add = [NSMutableArray array];
        }
        [add addObject:NaMe];
    }
    NSLog(@"%@",add);

此添加显示我的单元格名称,我希望将此名称存储在.plist文件中。

4 个答案:

答案 0 :(得分:2)

我假设你想保存plist以保持持久性。

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

[add writeToFile:filePath atomically:YES];

从plist读回来

NSArray *array = [NSArray arrayWithContentsOfFile:filePath];

答案 1 :(得分:1)

NSArray*pListpaths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,  YES);
NSString*pListdocumentsDirectory = [pListpathsobjectAtIndex:0];
NSString*pListpath = [pListdocumentsDirectory stringByAppendingPathComponent:@"Apps.plist"]; NSFileManager*pListfileMgr = [NSFileManager defaultManager];
     //Create a plist if it doesn't alread exist
if (![pListfileMgrfileExistsAtPath: pListpath])
{
     NSString*bundle = [[NSBundle mainBundle]pathForResource:@"Apps" ofType:@"plist"];
    [pListfileMgrcopyItemAtPath:bundletoPath: pListpatherror:&error]; 
}

 //Write to the plist
NSMutableDictionary*thePList = [[NSMutableDictionary alloc] initWithContentsOfFile: pListpath]; 
[thePList setObject:[NSString stringWithFormat:@"YourContent"] forKey:@"Related Key"];
[thePList writeToFile: pListpathatomically: YES];

试试此示例代码

答案 2 :(得分:0)

解决方案非常简单。 创建一个包含NSMutableArray的NSArray,然后将其写入路径。

NSArray *yourArray=[NSArray arrayWithObjects:<#(id), ...#>, nil];

NSArray *yourPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libDir = [yourPath objectAtIndex:0];
NSString *loc = [libDir stringByAppendingString:@"/anyfilename.plist"];
[yourArray writeToFile:loc atomically:YES];

使用以下方法获取数组:

yourPath = [bundle pathForResource:@"anyfilename" ofType:@"plist"];
yourArray = (yourArray!= nil ? [NSArray arrayWithContentsOfFile:loc] : nil);

答案 3 :(得分:0)

使用此代码

#define DOC_DIR [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]

NSArray *NaMe;
NSMutableArray *add;
NSMutableArray *all;
for (int i =0; i<11; i++) {
    NSIndexPath *indexPath = [self.Table indexPathForSelectedRow];
    NaMe = [[all objectAtIndex:(indexPath.row)+i]objectForKey:@"name"];
    if(!add){
        add = [NSMutableArray array];
    }
    [add addObject:NaMe];
}
NSLog(@"%@",add);
[self writeDataToPlistFromArray:add];

-(void) writeDataToPlistFromArray:(NSArray  *) dataArray
{

    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:dataArray,@"Root", nil];
    NSString *path = [DOC_DIR stringByAppendingPathComponent:@"Names.plist"];
    [dic writeToFile:path atomically:YES];
}