-2

NSMutableArray的名字“add”在自己的名字中cell(in UITableView)我想把这个“add”存储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

4 回答 4

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];
于 2013-04-22T11:32:53.017 回答
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];

试试这个示例代码

于 2013-04-22T11:38:36.203 回答
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];
}
于 2013-04-22T11:46:31.677 回答
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);
于 2013-04-22T11:36:41.980 回答