I create a Bar button item (trash can) using storyboard.App crashes when i try to delete a file from document directory on button action. Here is code so far:
我使用storyboard.App创建了一个Bar按钮项(垃圾桶),当我尝试从按钮操作的文档目录中删除文件时崩溃。这是到目前为止的代码:
@property (retain, nonatomic) IBOutlet UIBarButtonItem *deleteCsv;
-(void)deleteCsv:(id)sender{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:fileDataString4];
NSError *error;
BOOL success = [fileManager removeItemAtPath:filePath error:&error];
if (success) {
UIAlertView *removedSuccessFullyAlert = [[UIAlertView alloc] initWithTitle:@"Congratulations:" message:@"Successfully removed" delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
[removedSuccessFullyAlert show];
}
else
{
NSLog(@"Could not delete file -:%@ ",[error localizedDescription]);
}
1 个解决方案
#1
1
I think you should check first whether file exist at path or not with following code,
我想你应该首先检查文件是否存在于路径中,代码如下:
if ([[NSFileManager defaultManager] fileExistsAtPath:[documentsDirectory stringByAppendingPathComponent:<your file path>]])
{
//Delete your file
}
By following this you will be able to handle the crash if it is occuring because of file nonexistence
通过遵循此操作,您将能够处理由于文件不存在而发生的崩溃
#1
1
I think you should check first whether file exist at path or not with following code,
我想你应该首先检查文件是否存在于路径中,代码如下:
if ([[NSFileManager defaultManager] fileExistsAtPath:[documentsDirectory stringByAppendingPathComponent:<your file path>]])
{
//Delete your file
}
By following this you will be able to handle the crash if it is occuring because of file nonexistence
通过遵循此操作,您将能够处理由于文件不存在而发生的崩溃