读取文件中的文件,否则返回零
我正在阅读用户在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了这个问题并解决了问题。我应该使用
didpickdocumentat
而不是didpickdocumentsat
,然后请求访问。这是新代码:I found the issue and solved it. I should have used
didPickDocumentAt
instead ofdidPickDocumentsAt
and then request access. Here is the new code: