removeItemAtPath在设备上不起作用 [英] removeItemAtPath dosn't work on the device

查看:225
本文介绍了removeItemAtPath在设备上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为此苦苦挣扎了一段时间,因此欢迎任何提示或建议.

I'v been struggling with this one for some time so any hint or suggestion are welcome.

我正在尝试从文档"下的目录中删除文件.问题是文件并未在设备上被删除,而是在模拟器上被删除. 只是为了增加神秘性,在调用removeItemAtPath之前,我检查文件是否存在fileExistsAtPath,甚至显示该文件夹下的文件列表.

I'm trying to delete a file from a directory under "Documents". The problem is that the file is not delete on the device dow it is on the simulator. Just to add to the mystery, before the call to removeItemAtPath I check if the file exists with fileExistsAtPath, and even display the list of files under that folder.

附加了要删除的代码:

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

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

The code that display the list of files in that folder...

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

    }

非常感谢.

推荐答案

此:

[[NSBundle mainBundle] resourcePath]

...返回如下路径:

... returns a path like:

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

...,这是指向只读应用程序捆绑包的路径.编译应用程序后,其捆绑包内容无论如何都无法更改.

...which is the path to the app bundle itself which is read only. Once an app is complied its bundle contents are unalterable in anyway.

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

To find a file in the documents folder use:

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

...返回的路径如下:

... which returns a path like:

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

添加文件名,一切顺利.

Append the file name and you are good to go.

这篇关于removeItemAtPath在设备上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆