Delete specified file from document directory

前端 未结 10 942
礼貌的吻别
礼貌的吻别 2020-12-02 06:18

I want to delete an image from my app document directory. Code I have written to delete image is:

 -(void)removeImage:(NSString *)fileName
{
    fileManag         


        
10条回答
  •  温柔的废话
    2020-12-02 06:33

    Swift 2.0:

    func removeOldFileIfExist() {
        let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
        if paths.count > 0 {
            let dirPath = paths[0]
            let fileName = "someFileName"
            let filePath = NSString(format:"%@/%@.png", dirPath, fileName) as String
            if NSFileManager.defaultManager().fileExistsAtPath(filePath) {
                do {
                    try NSFileManager.defaultManager().removeItemAtPath(filePath)
                    print("old image has been removed")
                } catch {
                    print("an error during a removing")
                }
            }
        }
    }
    

提交回复
热议问题