在展开 NSData 类型的可选值时意外发现 nil

Unexpectedly found nil while unwrapping an optional value of type NSData

nil 的值是变量路径,我在我的项目中链接了文件,我需要帮助了解我在这里做错了什么。我找不到重复的问题,但如果有人链接它并将其标记为重复,我将不胜感激,否则提前谢谢你。

func Upload() {
    var path = NSBundle.mainBundle().pathForResource("/Users/matthewcarlson/Library/Mobile Documents/com~apple~CloudDocs/LiViD/LiViD/big_buck_bunny_720p_2mb.mp4", ofType: "mp4")
    var videodata: NSData
    videodata = (NSData.dataWithContentsOfMappedFile(path!) as? NSData)!


    let file = PFFile(name:"resume.txt", data:videodata)
    file!.saveInBackground()

}

我认为您将完整路径作为路径资源的参数传递。只需将文件名作为参数传递。我认为是 big_buck_bunny_720p_2mb.

所以你的代码应该是这样的,

 var path = NSBundle.mainBundle().pathForResource("big_buck_bunny_720p_2mb", ofType: "mp4") // or your file's name only here

如果项目中只有一个 mp4 文件,那么您可以将 nil 作为 pathResource 传递,因为 Apple document 指出,

The name of the resource file, If you specify nil, the method returns the first resource file it finds with the specified extension.

希望这会有所帮助:)