removeItemAtPath 在设备上不起作用
Posted
技术标签:
【中文标题】removeItemAtPath 在设备上不起作用【英文标题】:removeItemAtPath dosn't work on the device 【发布时间】:2010-09-01 13:16:51 【问题描述】:我一直在努力解决这个问题,所以欢迎任何提示或建议。
我正在尝试从“文档”下的目录中删除文件。问题是该文件没有在设备上删除,而是在模拟器上。 更神秘的是,在调用 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】:这个:
[[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
附加文件名就可以了。
【讨论】:
以上是关于removeItemAtPath 在设备上不起作用的主要内容,如果未能解决你的问题,请参考以下文章
CMMotionManager:设备校准在真实设备上不起作用