• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Harley
博客园    首页    新随笔    联系   管理    订阅  订阅

文件、文件夹操作(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([])

 

参考:文件夹操作

 

posted @ 2018-05-20 22:19  Harely  阅读(332)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3