2013-04-22 63 views
-2

NSMutableArray名称为“加”即具有在cell(以UITableView) 自己的名字,我想这些信息存储在.plist文件“添加” NSMutableArray如何存储的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文件存储。

+0

http://stackoverflow.com/questions/9356929/how-to-save-nsmutable-array-into-plist-in-iphone – Pradeep 2013-04-22 11:34:21

+0

谁能指导我的源代码????? – john 2013-04-22 12:07:08

+0

你能使用搜索? – peko 2013-04-22 12:21:57

回答

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]; 
+0

我的朋友我应该在我的应用程序中创建.plist文件(Names.plist)? – john 2013-04-22 11:38:27

+0

我的朋友在哪里创建Names.plist ??? – john 2013-04-22 11:43:59

+1

我写这段代码,但没有储存在plist中!为什么??? – john 2013-04-22 11:44:35

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

尝试此示例代码

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]; 
} 
+0

没有工作! :( – john 2013-04-22 12:05:38

+0

不工作,不能写入现有的plist文件 – 2016-01-14 08:13:16

相关问题