2011-07-28 7 views
2

listOfContactsToLay라는 배열이 있습니다.이 객체에는 3 개의 문자열 필드가있는 Contact 객체가 들어 있습니다. 이 배열을 어떻게 파일에 성공적으로 쓸 수 있습니까? 여기 내가 뭘 찾았는지 : iPhone dev - 파일에 객체 배열 쓰기/읽기

-(NSString*)fPath 
{ 
    return [DocumentsDirectory() stringByAppendingPathComponent:@"quickdial.xml"]; 
} 

//SERIALIZATION 
- (void) save:(NSMutableArray*)array 
{ 

    [NSKeyedArchiver archiveRootObject:array toFile:[self fPath]]; 

    NSLog(@"List of contacts been written in file"); 

} 

// get that array back 
- (NSMutableArray*)load 
{ 
    NSLog(@"List of contacts is being read from file"); 
    return [NSKeyedUnarchiver unarchiveObjectWithFile:[self fPath]]; 

} 

그리고 물론

다음은 작동하지 않습니다 : 그것은 잘못된 인수 예외가 발생

-(IBAction)doneWithContacts { 
    [ContactManager createXmlInMydocumentsDirectory]; 
[self save:listOfContactsToLay]; 
    NSLog(@"List of contacts to lay saved!"); 
    [self.parentViewController dismissModalViewControllerAnimated:YES]; 

} 

.

어떻게해야합니까? 내 개체의 MSMutableArray를 어떻게 serialize합니까? 감사합니다.

답변

0

파일 형식이 마음에 드십니까? 그렇지 않다면 왜 NSArray 클래스의 내장 메서드를 사용하지 않을까요?

+ (id)arrayWithContentsOfFile:(NSString *)aPath; 
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag; 

NSArray Class Reference을 참조하십시오.

+0

을 클래스 메소드 인 arrayWithContentsOfFile : 또는 인스턴스 메소드 initWithContentsOfFile :. 이 메소드는 파일을 쓰기 전에 모든 포함 된 객체가 특성 목록 객체임을 반복적으로 확인하고, 결과 파일이 유효한 특성 목록이 아니기 때문에 모든 객체가 특성 목록 객체가 아닌 경우 NO를 리턴합니다. –