读取文件中的文件,否则返回零

发布于 2025-01-21 04:43:10 字数 851 浏览 1 评论 0原文

我正在阅读用户在iOS Swift中选择的 plist 文件。该代码在XCode模拟器上按预期工作(iPhone 13)。但是在真实的iPhone 13上,readArrayFromplist函数返回nil。我找不到原因。

  • 我已经从iPhone和iCloud中选择了文件选择器。
  • 我已经读到iOS在docucepicker函数中提供了权限。
  • 我已经打印了URL,看起来还不错(+在模拟器上起作用)。

这是代码(imagePosArray在Real iPhone上为零):

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
    guard let myURL = urls.first else {
        return
    }
    print("import result : \(myURL)")
    imagePosArray = readArrayFromPlist(filepath: myURL.path)
}

func readArrayFromPlist(filepath: String) -> [Int]? {
    if let arrayFromFile: [Int] = NSArray(contentsOfFile: filepath) as? [Int] {
        return arrayFromFile
    }
    return nil
}

I am reading a plist file selected by the user in iOS swift. The code works as expected on XCode simulators (iPhone 13). But on a real iPhone 13, the readArrayFromPlist function returns nil. I couldn’t find the reason.

  • I've selected the file using document picker both from my iPhone and iCloud.
  • I've read that iOS provides permission inside documentPicker function.
  • I've printed the URL and it looks OK (+ it works on simulators).

Here is the code (imagePosArray is nil on real iPhone):

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
    guard let myURL = urls.first else {
        return
    }
    print("import result : \(myURL)")
    imagePosArray = readArrayFromPlist(filepath: myURL.path)
}

func readArrayFromPlist(filepath: String) -> [Int]? {
    if let arrayFromFile: [Int] = NSArray(contentsOfFile: filepath) as? [Int] {
        return arrayFromFile
    }
    return nil
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆悲凉 2025-01-28 04:43:10

我找到了这个问题并解决了问题。我应该使用didpickdocumentat而不是didpickdocumentsat,然后请求访问。这是新代码:

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
    // Start accessing a security-scoped resource.
    guard url.startAccessingSecurityScopedResource() else {
        // Handle the failure here.
        return
    }

    print(url.path)
    imagePosArray = readArrayFromPlist(filepath: url.path)

    // Make sure you release the security-scoped resource when you finish.
    defer { url.stopAccessingSecurityScopedResource() }
}

I found the issue and solved it. I should have used didPickDocumentAt instead of didPickDocumentsAt and then request access. Here is the new code:

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
    // Start accessing a security-scoped resource.
    guard url.startAccessingSecurityScopedResource() else {
        // Handle the failure here.
        return
    }

    print(url.path)
    imagePosArray = readArrayFromPlist(filepath: url.path)

    // Make sure you release the security-scoped resource when you finish.
    defer { url.stopAccessingSecurityScopedResource() }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文