遍歷一個目錄下的所有文件 首先我們獲取用戶文檔目錄路徑 1 let manager = FileManager.default 2 let urlForDocument = manager.urls(for: .documentDirectory, in:.userDomainMask) 3 let ...
遍歷一個目錄下的所有文件
首先我們獲取用戶文檔目錄路徑
1 let manager = FileManager.default 2 let urlForDocument = manager.urls(for: .documentDirectory, in:.userDomainMask) 3 let url = urlForDocument[0] as URL 4 print("---------->\(url)")View Code
列印結果:
---------->file:///Users/huanggang/Library/Developer/CoreSimulator/Devices/7B9AE7A4-4838-495B-964D-560DF760F7E1/data/Containers/Data/Application/70D8DA8D-4FEF-4715-B82E-0F40C1BC1B78/Documents/
深度遍歷,會遞歸遍歷子文件夾
1 let enumeratorAtPath = manager.enumerator(atPath: url.path) 2 enumeratorAtPath?.skipDescendants() //跳過遞歸到最近獲得的子目錄 3 print("enumeratorAtPath: \(enumeratorAtPath?.allObjects)")View Code
列印結果:
enumeratorAtPath: Optional([])
參考:文件夾操作