maz maz - 6 months ago 17
Swift Question

Unexpectedly found nil while unwrapping an optional value of type NSData

The value that is nil is the variable path, I have the file linked within my my project, and I need help on what I am doing wrong here. I could not find a duplicate question but if there is one I would greatly appreciate if someone linked it and marked this as a duplicate, otherwise thank you in advance.

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()

}

Answer

I think you are passing full path as a parameter of path resource. Just pass name of file as parameter. I think it is big_buck_bunny_720p_2mb.

so your code should be like,

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

If you have only one mp4 file in project then you can pass nil as pathResource because as Apple document states,

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

Hope this will help :)

Comments