Tuesday, April 10, 2012

need to save contents of table view as txt file and the reload it later

i need to be able to save the contents of my table view which is given data by an NSMutableArray to a txt file and then need to reopen that file automatically when the window containing the table view. i am making an application for mac

thanks
this is the code for the data source:


#import "tableViewData.h"
 #import "Customer.h"
 @implementation tableViewData
 -(id) init
 {
 self = [super init];
 if (self) {
     list = nil;
     filepath = @"/Users/Gautambir/Desktop/CustomerNames.txt";
     if ([[NSFileManager defaultManager]fileExistsAtPath:filepath]) {
         list = [[NSMutableArray alloc] initWithContentsOfFile:filepath];
     }
     else
     list = [[NSMutableArray alloc]initWithObjects:name,memberNumber ,nil];
         [list writeToFile:filepath atomically:YES];
 }
 return self;
 }
 
 -(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{
 return [list count];
 }
 
 -(id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn     *)tableColumn row:(NSInteger)row{
 Customer *Customer = [list objectAtIndex:row];
 NSString *identifier = [tableColumn identifier];
 return [Customer valueForKey:identifier];
 }
 
 -(void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
 Customer *Customer = [list objectAtIndex:row];
 NSString *identifier = [tableColumn identifier];
 [Customer setValue:object forKey:identifier];
 }
 
 
 -(void)save{
 [list writeToFile:filepath atomically:YES];
 }
 -(IBAction)add:(id)sender{
 [list addObject:[[Customer alloc]init]];
 [tableView reloadData];
 
 NSLog (@"array:%@",list);
 
 
 }
 
 -(IBAction)remove:(id)sender{
 NSInteger row = [tableView selectedRow];
 if (row != -1) {
     [list removeObjectAtIndex:row];
 }
 
 [tableView reloadData];
 
 }
 
 
 
 
 -(void)dealloc
 {
 [super dealloc];
 }
 
 
 @end
 


and this is the .m file for the customer object class:

#import "Customer.h"


@implementation Customer
 
 @synthesize name;
 @synthesize memberNumber;
 
 
 -(id) init
 {
 self = [super init];
 if(self) {
     name = @"Test";
     int i = arc4random()%1000000000000000000;
     if (i<0) {
         memberNumber = i*-1;
     }
     else
         memberNumber = i;
 }
 return self;
 
 }
 
 -(void)dealloc
 {
 [name release];
 [super dealloc];
 }
 
 -(id)initWithCoder:(NSCoder *)aDecoder
 {
 self = [super init];
 if (self)
 {
     name = [[aDecoder decodeObjectForKey:@"name"]retain];
     memberNumber = [aDecoder decodeIntForKey:@"memberNumeber"];
 }
 return self;
 
 }
 
 -(void)encodeWithCoder:(NSCoder *)aCoder
 {
 [aCoder encodeObject:name forKey:@"name"];
 [aCoder encodeInt:memberNumber forKey:@"memberNumber"];
 }
 
 @end
 


strong text


Answer:

im sorry to post another answer - i misread the tags, and my first answer was relying to iOS. this is the way to do it in OSX:
saving
NSMutableArray *array = ...  //your array
[array addObject:...]; [array addObject:...];
[array addObject:...];
...
// then use an NSData to store the array
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];
NSString *path = @"/Users/User/path...";
[data writeToFile:path options:NSDataWritingAtomic error:nil];
retrieving
NSMutableArray *archive = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
sebastian

No comments:

Post a Comment