【问题标题】:FileManager.enumerator returns nil while testing on ios deviceFileManager.enumerator 在 ios 设备上测试时返回 nil
【发布时间】:2018-12-10 06:10:29
【问题描述】:
    let documentsPath = "Users/xxxx/Desktop/*******/iOSSimulator/"
    let url = URL(fileURLWithPath: documentsPath)
    let fileManager = FileManager.default
    print(FileManager.default.enumerator(atPath: url.path))

我想在实际的 iOS 设备上运行我的 UI 测试。但是在真实设备上执行测试用例时,我得到 nil(在打印语句中)。但是测试用例在模拟器上成功执行(这里我得到print 语句中的一些值)。

我无法弄清楚为什么它在 ios 设备中返回 nil 值。

【问题讨论】:

  • 设备上没有“/Users/xxxx/Desktop”目录...
  • 在真机上,不应该有任何iOSSimulator的东西。
  • 谢谢@Kamran。我得到了我正在做的错误。是否有任何公共位置可以放置我的文件,这些文件可以在测试用例执行期间拾取?

标签: ios swift xcode xcuitest


【解决方案1】:

您提供了错误的路径(let documentsPath = "Users/xxxx/Desktop/*******/iOSSimulator/")。 真正的 iOS 设备不存在这条路径,所以你得到了 nil。

【讨论】:

  • 谢谢@sanjeev。我在文档路径中创建了一些我在测试用例中使用的文件。是否有任何公共位置可以放置我的文件?
  • 请将您的文件添加到项目目录而不是系统目录中。在项目目录“stackoverflow.com/questions/45096410/…”中添加文件。现在您可以访问您的文件了。
【解决方案2】:

您正在提供静态文档路径,它可能存在于模拟器中但不存在于设备中。使用它来快速获取文档路径

 if let documentsPathString = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first {
            //This gives you the string formed path
    }

    if let documentsPathURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
            //This gives you the URL of the path
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-07
    • 2017-10-30
    • 2015-10-06
    • 2014-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多