列出指定文件夹的文件
1 let fileManager = FileManager.default() 2 3 let urlArray = fileManager.urlsForDirectory(FileManager.SearchPathDirectory.userDirectory, inDomains: FileManager.SearchPathDomainMask.allDomainsMask) 4 5 for url in urlArray { 6 print("url = \(url)") 7 8 let resKeys = [URLResourceKey.nameKey, URLResourceKey.isDirectoryKey] 9 10 let resKeysStr = [URLResourceKey.nameKey.rawValue, URLResourceKey.isDirectoryKey.rawValue] 11 12 let dirEnumerator = fileManager.enumerator(at: url, includingPropertiesForKeys: resKeysStr, options: [.skipsHiddenFiles,], errorHandler: nil)! 13 14 for case let fileUrl as NSURL in dirEnumerator { 15 guard let resValues = try? fileUrl.resourceValues(forKeys: resKeys), 16 let isDirectory = resValues[URLResourceKey.isDirectoryKey] as? Bool, 17 let name = resValues[URLResourceKey.nameKey] as? String 18 else { 19 continue 20 } 21 22 if isDirectory { 23 dirEnumerator.skipDescendants() 24 print("dir name is : \(name)") 25 } else { 26 print("file name is : \(name)") 27 } 28 } 29 30 }