NSFileManager delete contents of directory

后端 未结 9 1731
你的背包
你的背包 2021-01-30 17:13

How do you delete all the contents of a directory without deleting the directory itself? I want to basically empty a folder yet leave it (and the permissions) intact.

9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-30 17:51

    Swift 3 if anyone needs it for a quick cut/paste

    let fileManager = FileManager.default
    let fileUrls = fileManager.enumerator(at: folderUrl, includingPropertiesForKeys: nil)
    while let fileUrl = fileUrls?.nextObject() {
        do {
            try fileManager.removeItem(at: fileUrl as! URL)
        } catch {
            print(error)
        }
    }
    

提交回复
热议问题