NSFileHandle fileHandleForWritingAtPath: ritorno nullo!
-
01-10-2019 - |
Domanda
il mio iPad ha un piccolo impianto di download, per la quale voglio aggiungere i dati utilizzando un NSFileHandle. Il problema è la chiamata creazione restituisce solo handle di file nulli. Quale potrebbe essere il problema? Ecco le tre righe di codice che dovrebbero creare il mio gestore di file:
NSString *applicationDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
self.finalPath = [applicationDocumentsDirectory stringByAppendingPathComponent: self.fileName];
NSFileHandle *output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath];
ho controllato il percorso del file, e ho potuto vedere niente di sbagliato.
TYIA
Soluzione
fileHandleForWritingAtPath
non è una chiamata “creazione”. La documentazione afferma esplicitamente: “Valore di ritorno: l'handle di file inizializzato, o nil se nessun file esiste nel percorso ” (enfasi aggiunta). Se si desidera creare il file se non esiste, che avrebbe dovuto usare qualcosa di simile a questo:
NSFileHandle *output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath];
if(output == nil) {
[[NSFileManager defaultManager] createFileAtPath:self.finalPath contents:nil attributes:nil];
output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath];
}
Se si desidera aggiungere al file, se già esiste, uso qualcosa come [output seekToEndOfFile]
. Il tuo codice completo sarebbe poi apparire come segue:
NSString *applicationDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
self.finalPath = [applicationDocumentsDirectory stringByAppendingPathComponent: self.fileName];
NSFileHandle *output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath];
if(output == nil) {
[[NSFileManager defaultManager] createFileAtPath:self.finalPath contents:nil attributes:nil];
output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath];
} else {
[output seekToEndOfFile];
}
Altri suggerimenti
percorso di directory documenti
+(NSURL *)getDocumentsDirectoryPath
{
return [[[NSFileManager defaultManager]URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]lastObject];
}
Salva testo alla fine del file
se il file doesnt esistono crearlo e dati di scrittura
+(void)saveText:(NSString *)textTobeSaved atPath:(NSString*)fileName
{
NSString *filePath = [NSString stringWithFormat:@"%@.text",fileName];
NSString *path = [[self getDocumentsDirectoryPath].path
stringByAppendingPathComponent:filePath];
NSFileHandle *fileHandler = [NSFileHandle fileHandleForWritingAtPath:path];
if(fileHandler == nil) {
[[NSFileManager defaultManager] createFileAtPath:path contents:nil attributes:nil];
fileHandler = [NSFileHandle fileHandleForWritingAtPath:path];
} else {
textTobeSaved = [NSString stringWithFormat:@"\n-----------------------\n %@",textTobeSaved];
[fileHandler seekToEndOfFile];
}
[fileHandler writeData:[textTobeSaved dataUsingEncoding:NSUTF8StringEncoding]];
[fileHandler closeFile];
}
Testo ottenere dal file con il nome del file specificato
+(NSString *)getTextFromFilePath:(NSString *)fileName
{
NSString *filePath = [NSString stringWithFormat:@"%@.text",fileName];
NSString *path = [[self getDocumentsDirectoryPath].path
stringByAppendingPathComponent:filePath];
NSLog(@"%@",path);
if(path!=nil)
{
NSString *savedString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
return savedString;
}else{
return @"";
}
}
Elimina file
+(void)deleteFile:(NSString *)fileName
{
NSString *filePath = [NSString stringWithFormat:@"%@.text",fileName];
NSString *path = [[self getDocumentsDirectoryPath].path
stringByAppendingPathComponent:filePath];
NSFileHandle *fileHandler = [NSFileHandle fileHandleForWritingAtPath:path];
if(fileHandler != nil) {
[[NSFileManager defaultManager]removeItemAtPath:path error:nil];
}
}