like to save users personal info (name, phone number etc) to a file using: NSKeyedArchiver
for later use.
Getting following error when I click on SaveButton
:
2012-03-30 14:01:11.483 Archive2[41840:f803] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSArrayM insertObject:atIndex:]: object cannot be nil' * First throw call stack: (0x13c8022 0x1559cd6 0x13b533e 0x13b61f0 0x2917 0x13c9e99 0x1514e 0x150e6 0xbbade 0xbbfa7 0xbb266 0x3a3c0 0x3a5e6 0x20dc4 0x14634 0x12b2ef5 0x139c195 0x1300ff2 0x12ff8da 0x12fed84 0x12fec9b 0x12b17d8 0x12b188a 0x12626 0x1f9d 0x1f05) terminate called throwing an exception(lldb)
h file:
@property (nonatomic, strong) IBOutlet UITextField *vorname;
@property (nonatomic, strong) IBOutlet UITextField *nachname;
@property (nonatomic, strong) IBOutlet UITextField *ausweisNR;
@property (nonatomic, strong) IBOutlet UITextField *serviceNR;
@property (nonatomic, strong) IBOutlet UITextField *telephoneNumber;
@property (nonatomic, strong) NSString *dataFilePath;
-(IBAction)saveData:(id)sender;
m file:
- (void)viewDidLoad
{
[super viewDidLoad];
NSFileManager *filemgr;
NSString *docsDir;
NSArray *dirPaths;
filemgr = [NSFileManager defaultManager];
//Get the documents directory:
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Build the path to the data file:
dataFilePath = [[NSString alloc]initWithString:[docsDir stringByAppendingPathComponent:@"data.archive"]];
// Check if the file already exist:
if ([filemgr fileExistsAtPath:dataFilePath]) // File exist so rebuild...
{
NSMutableArray *userCredentials;
userCredentials = [NSKeyedUnarchiver unarchiveObjectWithFile:dataFilePath];
self.serviceNR.text = [userCredentials objectAtIndex:0];
self.ausweisNR.text = [userCredentials objectAtIndex:1];
self.vorname.text = [userCredentials objectAtIndex:2];
self.nachname.text = [userCredentials objectAtIndex:3];
self.telephoneNumber.text = [userCredentials objectAtIndex:4];
}
}
-(IBAction)saveData:(id)sender
{
NSMutableArray *userCredentials = [[NSMutableArray alloc]init];
[userCredentials addObject:self.serviceNR.text];
[userCredentials addObject:self.ausweisNR.text];
[userCredentials addObject:self.vorname.text];
[userCredentials addObject:self.nachname.text];
[userCredentials addObject:self.telephoneNumber.text];
[NSKeyedArchiver archiveRootObject:userCredentials toFile:dataFilePath];
}
One of your text fields is empty. You should test them first.
if([self.serviceNR.text length]){
[userCredentials addObject:self.serviceNR.text];
}
and so on....