2012-07-30 4 views
0
을 ArchiveRootObject
NSMutableArray *esami = [[NSMutableArray alloc] init]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains 
    (NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    //make a file name to write the data to using the 
    //documents directory: 
    NSString *fullFileName = [NSString stringWithFormat:@"%@/new", documentsDirectory]; 
    esami = [NSKeyedUnarchiver unarchiveObjectWithFile:fullFileName]; 
    Esame *add=[[Esame alloc]initWithNome:materia voto:voto crediti:crediti anno:anno]; 
    [esami addObject:add]; 
    [NSKeyedArchiver archiveRootObject:esami toFile:fullFileName]; 

이미 내 루트 디렉토리로 작성된 파일을 사용하는 경우이 코드는 .. 작동하지 않습니다 내 배열에 새 요소를 추가하고 쓸 수 있습니다 내가 왜 understad 수 없습니다 하지만 다른 파일 (이 경우 새로운 파일)을 사용하면 작동하지 않습니다.쓰기 배열

+0

바퀴를 재발 명하지 마십시오. NSString * fullFileName = [documentsDirectory stringByAppendingPathComponent : @ "new"]; ' –

+0

괜찮습니다.하지만 아무 것도 바뀌지 않습니다! –

+1

그게 내가 이것을 asnwer라고 덧붙여 말하지 않은 이유입니다. –

답변

0

나는 이유를 알고하지 않습니다하지만이 코드

NSMutableArray *esami = [NSKeyedUnarchiver unarchiveObjectWithFile:fullFileName]; 

if(esami.count==0){ 
    NSMutableArray *esamiNew=[[NSMutableArray alloc] init]; 
    [esamiNew addObject:[[Esame alloc]initWithNome:materia voto:voto crediti:crediti anno:anno]]; 

    [NSKeyedArchiver archiveRootObject:esamiNew toFile:fullFileName]; 

} 
else{ 

    [esami addObject:[[Esame alloc]initWithNome:materia voto:voto crediti:crediti anno:anno]]; 
    NSLog(@"num %d",esami.count); 
    [NSKeyedArchiver archiveRootObject:esami toFile:fullFileName]; 

} 

합니까이 버그로이 문제를 해결?