【问题标题】:FileManager not working on iOS 7FileManager 在 iOS 7 上不起作用
【发布时间】:2018-03-07 14:12:08
【问题描述】:

我最近将 Xcode 更新到最新版本 (Xcode9)。更新后,在 iOS 7 中,当我尝试使用 FileManager 获取应用程序目录的 URL 时,它会崩溃 这是我的代码,它在 xcode 更新之前运行。

    var error:NSError?
    let manager = FileManager.default
    do {
        let docURL = try manager.url(for: .documentDirectory, in:.userDomainMask, appropriateFor:nil, create:true);
    } catch let error1 as NSError {
        error = error1
    }

【问题讨论】:

  • 注意:不要像这样声明error 变量,你不需要它。同时删除let error1 as NSError。只要有一个catch 并在块内使用自动生成 error 变量。
  • iOS7? iOS 7 下如何用 Swift 编写代码?
  • 2% 的设备使用的是低于 9 的 iOS 版本。如果您的项目不需要专门在 iOS 7 上运行,则应该使用它。

标签: ios swift ios7 nsfilemanager xcode9


【解决方案1】:

那个 API 不再是 throws 而是提供了一个类似 [URL] 的数组,然后我们可以使用该数组中的 first 元素供我们使用。

用法:

if let docURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
    // Use docURL
} 

【讨论】:

  • 它会崩溃还是被“抓住”?
猜你喜欢
  • 2016-09-14
  • 2013-10-19
  • 1970-01-01
  • 2013-10-19
  • 2013-09-16
  • 2013-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多