文件、文件夹操作(I)

遍历一个目录下的所有文件

首先我们获取用户文档目录路径

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([])

 

猜你喜欢

转载自www.cnblogs.com/EchoHG/p/9064880.html
今日推荐