Deleting all the files in the iPhone sandbox (documents folder)?

前端 未结 7 2123
清歌不尽
清歌不尽 2020-11-28 05:50

Is there an easy way to delete all the files(images) I saved in the documents folder of the app?

7条回答
  •  佛祖请我去吃肉
    2020-11-28 06:49

    For Swift devs:

    let fileMgr = NSFileManager()
    let dirPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
    if let directoryContents = try? fileMgr.contentsOfDirectoryAtPath(dirPath)
    {
        for path in directoryContents 
        {   
            let fullPath = (dirPath as NSString).stringByAppendingPathComponent(path)
            do 
            {
                try fileMgr.removeItem(atPath: fullPath)
                print("Files deleted")
            } 
            catch let error as NSError 
            {
                print("Error deleting: \(error.localizedDescription)")
            }
        }
    }
    

提交回复
热议问题