gpt4 book ai didi

swift - NSURL 错误 - 文件不存在

转载 作者:可可西里 更新时间:2023-11-01 02:16:51 26 4
gpt4 key购买 nike

我有一个问题。

当我执行 print("Video URL:\(self.videoUrl.description)") 时,我在输出中得到了这个:

Video URL: file:///var/mobile/Containers/Data/Application/92BDA035-131C-4FD2-A176-25F77C1D295B/Documents/video.mp4

我正在使用它来将文件上传到服务器:

func uploadVideo(){
let currentUploads = PFObject(className: "myClassOne")
currentUploads.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
if error == nil{
//**Success saving, now save video.**//

currentUploads["userFile"] = try! PFFile(name: "video.mp4", contentsAtPath: self.videoUrl.description)
currentUploads.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
if error == nil{
// Take user home
print("Successfully uploaded")
})
}
else{
print(error, terminator: "")
}
})
}
else{
print(error, terminator: "")
}
})
}

我收到这个错误:

fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=4 "Failed to create PFFile at path 'file:///var/mobile/Containers/Data/Application/92BDA035-131C-4FD2-A176-25F77C1D295B/Documents/video.mp4': file does not exist." UserInfo={NSLocalizedDescription=Failed to create PFFile at path 'file:///var/mobile/Containers/Data/Application/92BDA035-131C-4FD2-A176-25F77C1D295B/Documents/video.mp4': file does not exist.}: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-703.0.18.1/src/swift/stdlib/public/core/ErrorType.swift, line 54

怎么了?为什么我从 println 成功获取到 url 后却提示“文件不存在”?有什么想法/建议吗?

最佳答案

try! 表达式中使用 self.videoUrl.path 而不是 self.videoUrl.description ,如下所示:

currentUploads["userFile"] = try! PFFile(name: "video.mp4", contentsAtPath: self.videoUrl.path)

(videoUrl.description 是 url 的字符串表示,开头有 "file://" 格式,使其成为无效路径。在相反,videoUrl.path 是一个以 "/var/mobile/..." 开头的字符串。)

关于swift - NSURL 错误 - 文件不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37424829/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com