我做了一个类方法来读取 plist 文件,它工作正常。我在 ARC 中进行了更改,现在课程看起来像:
+ (NSDictionary *)readPlistDic:(NSString*)filePlist
{
NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [[NSString alloc] initWithString:[[documentsDirectoryPath stringByAppendingPathComponent:filePlist]stringByAppendingPathExtension:@"plist"]];
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *startPath = [[path stringByAppendingPathComponent:filePlist]stringByAppendingPathExtension:@"plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]){
if([[NSDictionary dictionaryWithContentsOfFile:startPath] writeToFile:filePath atomically:NO])
NSLog(@"File dei profili Copiato nell App DocumentsDirectory\n\n");
else
NSLog(@"Errore durante la copia in DocumentsDirectory\n\n");
}
NSDictionary *dicReturn = [[NSDictionary alloc] initWithContentsOfFile:filePath];
return dicReturn;
#warning return [NSDictionary dictionaryWithContentsOfFile:filePath];
}
它可以工作,但会产生泄漏
我错了什么?有什么建议吗?非常感谢!
更新 我在这里写plist:
NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:[ReadWritePlistNew readPlistDic:@"PartitaDefault"]];
NSDate *dataOggi = [NSDate date];
[dic setValue:dataOggi forKey:Data_Partita];
NSNumber *idPartitaNum = [NSNumber numberWithInt:ultimoID+1];
[dic setValue:idPartitaNum forKey:ID_Partita];
NSArray *salvaGioc = [NSArray arrayWithObjects:ID_GIOCATORE_NORD,ID_GIOCATORE_SUD,ID_GIOCATORE_EST,ID_GIOCATORE_OVEST, nil];
for (int i=0; i<4; i++) {
[dic setValue:[NSNumber numberWithInt:[[[self.players objectAtIndex:i] objectForKey:defID_Player] intValue]] forKey:[salvaGioc objectAtIndex:i]];
}
NSNumber *numGiocatori = [NSNumber numberWithInt:numeroGiocatori];
[dic setValue:numGiocatori forKey:GIOCATORI_NUM];
[dic setValue:[NSNumber numberWithInt:tavoloNumero] forKey:TAVOLO_NUMERO];
[dic setValue:[NSNumber numberWithBool:isEXP] forKey:EXP_MODE];
[dic setValue:[NSNumber numberWithBool:isVPAttivoLocal] forKey:VictoryPointAttivo];
if (isVPAttivoLocal) {
[dic setValue:[NSNumber numberWithInt:numeroSmazzateIndex] forKey:NumeroSmazzateIndex];
}
//GPS#warning salvare gps
//NSLog(@"isCustomVP_Local %@",isCustomVP_Local ? @"Attivo":@"Non attivo");
[dic setValue:[NSNumber numberWithBool:isCustomVP_Local] forKey:ISVP_CUSTOM];
[dic setValue:[NSString stringWithFormat:@"%d",puntiMax] forKey:PUNTI_FINE_PARTITA];
//salva tutto sul plist database
NSMutableDictionary *dicOld = [[NSMutableDictionary alloc] initWithDictionary:[ReadWritePlistNew readPlistDic:@"PartiteDB"]];
NSMutableArray *arrToSave = [[NSMutableArray alloc] initWithArray:[dicOld objectForKey:@"Partite"]];
//NSLog(@"dic: %@",dic);
[arrToSave addObject:dic];
[dicOld setValue:arrToSave forKey:@"Partite"];
[ReadWritePlistNew writeToPlist:@"PartiteDB" dic:dicOld];
和[ReadWritePlistNew writeToPlist:]:
+ (void)writeToPlist:(NSString*)filePlist arr:(NSArray*)arr
{
NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [[documentsDirectoryPath stringByAppendingPathComponent:filePlist]stringByAppendingPathExtension:@"plist"];
[arr writeToFile:filePath atomically:YES];
}