removeItemAtPath在设备上不起作用

时间:2010-09-01 13:16:52

标签: iphone device target nsfilemanager

我一直在为这个问题苦苦挣扎,所以欢迎任何提示或建议。

我正在尝试从“Documents”下的目录中删除文件。问题是该文件在模拟器上没有被删除。 只是为了添加神秘感,在调用removeItemAtPath之前,我检查文件是否存在fileExistsAtPath,甚至显示该文件夹下的文件列表。

附上删除代码:

// 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   
}

显示该文件夹中文件列表的代码......

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

    }

非常感谢。

1 个答案:

答案 0 :(得分:4)

此:

[[NSBundle mainBundle] resourcePath]

...返回如下路径:

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

...这是应用程序包本身的路径,它是只读的。一旦应用程序被编译,其捆绑内容无论如何都是不可更改的。

要在文档文件夹中查找文件,请使用:

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

...返回如下路径:

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

附加文件名,你很高兴。