-2

I have NSMutableArray with name "add" that has in self name of cell (in UITableView) I want store this "add" NSMutableArray in .plist file.

this is "add" code:

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

this add show me name of cell and I want store this name in .plist file.

Girish
  • 4,692
  • 4
  • 35
  • 55
john
  • 13
  • 1
  • 4

4 Answers4

2

I assume you want to save the plist for persistence.

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

[add writeToFile:filePath atomically:YES];

To read back from plist

NSArray *array = [NSArray arrayWithContentsOfFile:filePath];
Anupdas
  • 10,211
  • 2
  • 35
  • 60
  • my friend I should create .plist file (Names.plist) where in my application??? – john Apr 22 '13 at 11:38
  • my friend where create Names.plist??? – john Apr 22 '13 at 11:43
  • 1
    I write this code but not store in plist!!! why??? – john Apr 22 '13 at 11:44
  • 1
    @john This code will store a plist, just not somewhere that you can see right away. In iOS applications are sandboxed, and can not write to their own bundle, so you won't see the plist pop up in your Xcode project. You can however log `filePath`, go over to Finder, use command+g, paste filePath, and be taken directly to the app's documents directory in which you'll find the plist. – Mick MacCallum Apr 22 '13 at 11:54
  • @john After saving the array to plist, what do you want to do with it. The question was "How to save array to plist", this code exactly does that. Please edit your question for better answers. – Anupdas Apr 22 '13 at 12:27
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];

Try This Sample Code

Manish Agrawal
  • 10,958
  • 6
  • 44
  • 76
iCoder
  • 1,298
  • 1
  • 9
  • 25
0

The solution is quite simple. Create an NSArray containing your NSMutableArray, then write it to a path.

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

Fetch your array using:

yourPath = [bundle pathForResource:@"anyfilename" ofType:@"plist"];
yourArray = (yourArray!= nil ? [NSArray arrayWithContentsOfFile:loc] : nil);
Anil
  • 1,028
  • 9
  • 20
0

user this code

#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];
}
Amzy
  • 56
  • 2