NSBundle.mainBundle().URLForResource("bach1", withExtension: "jpg") returning null

swift

Solution

Your problem is that NSBundle.mainBundle().URLForResource("bach1", withExtension: "jpg") returns an optional NSURL. You need to use if let to unwrap it and extract your file path from the returned url as follow:

if let resourceUrl = NSBundle.mainBundle().URLForResource("bach1", withExtension: "jpg") {
    if NSFileManager.defaultManager().fileExistsAtPath(resourceUrl.path!) {
        print("file found")
    }
}

Problem

``` NSBundle.mainBundle().URLForResource("bach1", withExtension: "jpg") ``` The above code is returning null. In order to check if the file exists or not, I used below code: ``` let fileManager = NSFileManager.defaultManager() if fileManager.fileExistsAtPath(savepath) { println("exist") } ``` The above code returns that file exists in directory. So I don't understand why the first code is returning null

Original source