0
    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];

我无法理解为什么这段代码不起作用..如果我使用已经写在我的根目录中的文件,我可以将新元素添加到我的数组中并写入它但是如果我使用另一个文件(在这种情况下是新的)它不起作用。

4

1 回答 1

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

}

难道是bug??

于 2012-07-30T10:41:01.787 回答