2012-03-30 6 views
0

나중에 사용하기 위해 NSKeyedArchiver을 사용하여 사용자 개인 정보 (이름, 전화 번호 등)를 파일에 저장하는 것을 좋아합니다.NSKeyedArchiver를 통해 파일에 사용자 데이터를 저장하려고합니다 - NSInvalidArgumentException은 저장을 클릭하면 얻을 수 있습니다.

내가 SaveButton를 클릭하면 오류를 다음과 같은 방법 :

2012-03-30 14 : 01 : 11.483으로 Archive2 '* : [41840 : F803] * 응용 프로그램을 종료 인한 캐치되지 않는 예외'NSInvalidArgumentException ', 이유에 - [__ NSArrayM은 insertObject : atIndex :] '객체가 될 수 없다 닐 * 우선 투사 호출 스택 : (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) called throwing 종료 예외 (lldb)

시간 파일 :

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

미터 파일 : 텍스트 필드의

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

마크 : 당신은 변경 가능한 배열에 전무 값을 삽입하기 위해 노력하고 있습니다. 배열에 데이터를 추가하기 전에 유효성 검사를 시도하십시오. –

+0

유효성 검사를 먼저 수행하십시오. 빠른 도움에 감사드립니다. –

답변

1

하나는 비어 있습니다. 먼저 테스트해야합니다.

if([self.serviceNR.text length]){ 
    [userCredentials addObject:self.serviceNR.text]; 
} 

등등 ....

+0

유효성 검사를 먼저 수행하십시오. 빠른 도움에 감사드립니다. –