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

I can't understad why this code doesn't work.. If I use a file already written in my root directory I can add the new element to my array and write it but if I use another file (new in this case) it doesn't work.

Janak Nirmal
  • 22,706
  • 18
  • 63
  • 99
Usi Usi
  • 2,967
  • 5
  • 38
  • 69
  • Don't reinvent the wheel. It should be `NSString *fullFileName = [documentsDirectory stringByAppendingPathComponent:@"new"];` –  Jul 30 '12 at 09:54
  • It's ok but it doesn't change anything! – Usi Usi Jul 30 '12 at 10:05
  • 1
    I know, that's why I didn't add this as an asnwer but as a comment. –  Jul 30 '12 at 10:07

1 Answers1

0

I don't know why but I resolved this issue with this code

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

}

Does is it a bug??

Usi Usi
  • 2,967
  • 5
  • 38
  • 69