Domanda

I'v stato alle prese con questo per un certo tempo in modo che qualsiasi accenno o suggerimento sono i benvenuti.

Sto cercando di eliminare un file da una directory sotto "Documenti". Il problema è che il file non è cancellare sul dispositivo dow è sul simulatore. Giusto per aggiungere al mistero, prima della chiamata a controllo removeItemAtPath I se il file esiste con fileExistsAtPath, e anche visualizzare l'elenco dei file in quella cartella.

In allegato il codice per l'eliminazione:

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

if (editingStyle == UITableViewCellEditingStyleDelete) {

// Delete file
NSString *directory = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Documents/MyDir"];
NSString *path = [directory stringByAppendingPathComponent:[mListOfItems objectAtIndex:indexPath.row]];
NSError *error = nil;

// Check if FileName doesn't exist.
NSFileManager *fileManager = [NSFileManager defaultManager];

if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"File Deletion Failed" message:[NSString stringWithFormat:@"File %@ not found.", [mListOfItems objectAtIndex:indexPath.row]]  delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
 [alertView show];
 [alertView release];   
}
else {

  FAILED HERE ---V  
        if (![fileManager removeItemAtPath:path error:&error]) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error While Deleting File" message:[NSString stringWithFormat:@"%@", error]  delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];   

 }
 else {
// delete row from local data
[self.tableView beginUpdates];
[mListOfItems removeObjectAtIndex:indexPath.row];

// Delete the row from view
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView endUpdates];
 }// Delete - success
}// File Exist
}// Edit - Delete   
}

Il codice che visualizza l'elenco dei file in quella cartella ...

   - (void) handleFilesRowTapped
 {

 // Get list of files to be displayed
 NSFileManager *manager = [NSFileManager defaultManager];
   NSError *error = nil;
 NSString * directory = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Documents/MyDir"];
 NSArray *fileList = [manager contentsOfDirectoryAtPath:directory error:&error];

 // Allocate file list view controller
 FileListViewController *fileListViewController = [[FileListViewController alloc] initWithNibName:@"FileListViewController" bundle:nil];

 //// Make sure that only *.TXT files are displayed

 // Set the list to be displayed
 fileListViewController.mListOfItems = [[NSMutableArray alloc] init];
 for (NSString *fileName in fileList) {
  if (([[fileName substringWithRange:NSMakeRange([fileName length] - 4, 4)]     compare:@".txt"] == NSOrderedSame) || 
 ([[fileName substringWithRange:NSMakeRange([fileName length] - 4, 4)] compare:@".TXT"] == NSOrderedSame)){
   [fileListViewController.mListOfItems addObject:fileName];
  }
 }

 // Configure viwe controller
 [fileListViewController setTitle:@"Files"];

 // Push for display
 [self.navigationController pushViewController:fileListViewController animated:YES];
 [fileListViewController release];

    }

Molte grazie.

È stato utile?

Soluzione

Questa:

[[NSBundle mainBundle] resourcePath]

... restituisce un percorso simile:

~/Library/Application Support/iPhone Simulator/4.0.1/Applications/6FACD1C6-3E81-4FE1-97E0-F80604E134E0/i4TestBed.app

... che è il percorso per l'applicazione pacchetto in sé, che è di sola lettura. Una volta che un'applicazione viene rispettato il contenuto del bundle sono inalterabili in ogni caso.

Per trovare un file nella cartella Documenti uso:

NSArray *docPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath=[docPaths objectAtIndex:0];

... che restituisce un percorso simile:

~/Library/Application Support/iPhone Simulator/4.0.1/Applications/6FACD1C6-3E81-4FE1-97E0-F80604E134E0/Documents

Aggiungere il nome del file e vi sono buone per andare.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top