文件删除代码

时间:2013-08-27 11:09:38

标签: ios directory uicollectionview delete-file

我想使用从目录文件夹中获取照片的CollectionView删除图片,因为我已创建子目录来存储图像。

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"ray:%d", [Trash count]);
    NSString *trashBin = [Trash objectAtIndex:indexPath.row];
    NSLog(@"k%@l",trashBin);


}

这是我的删除代码,但我必须添加我不知道的文件删除代码。

1 个答案:

答案 0 :(得分:1)

我相信以下是您正在寻找的。

NSError *error;
NSString *myFileName;
// this is global variable


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"ray:%d", [Trash count]);
    NSString *myFileName = [Trash objectAtIndex:indexPath.row];

    NSLog(@"k%@l",myFileName);
    [self deleteMyFiles];



}

-(void) deleteMyFiles {
    NSFileManager *fileMgr = [NSFileManager defaultManager];

    // Point to Document directory
    NSString *documentsDirectory = [NSHomeDirectory() 
         stringByAppendingPathComponent:@"Documents"];

    NSString *filePath2 = [documentsDirectory 
                             stringByAppendingPathComponent:myFileName];

    if ([fileMgr removeItemAtPath:filePath2 error:&error] != YES)
      NSLog(@"Unable to delete file: %@", [error localizedDescription]);
}

有关详细信息,请访问

http://iosdevelopertips.com/data-file-management/iphone-file-system-creating-renaming-and-deleting-files.html

此链接包含要对文件执行操作的所有示例。


你得到这个是因为你没有设置错误。

NSError *error;

之前添加NSError *myFileName;
NSError *error;
NSString *myFileName;
// this is global variable