1

I ve just upgrade from Swift 2 to Swift 3, and i m facing a new challenge...

I have a player which run perfectly before, but now i have this following issue : "unexpectedly found nil while unwrapping an Optional value"

Here is my code :

print(audioselectionne)

let alertSound = URL(fileURLWithPath: Bundle.main.path(forResource: audioselectionne as String, ofType: "mp3")!)

I ve got : Optional("tiesto") and the crash...

I really dont understand where is the issue...

Thanks for the help.

zoul
  • 102,279
  • 44
  • 260
  • 354
user2971617
  • 63
  • 1
  • 9

2 Answers2

1

You should unwrap the optional, perhaps with optional binding.

BTW, you shouldn't be use path strings at all anymore. Just use the URL directly, e.g.

guard let resource = audioselectionne, let alertSound = Bundle.main.url(forResource: resource, withExtension: "mp3") else {
    // handle file not found here
    return
}

// use alertSound here
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • audioselectionne coming from a prepareforsegue and declared like this var audioselectionne:String? – user2971617 Nov 24 '16 at 22:32
  • i guess it s same probleme here... http://stackoverflow.com/questions/39537177/swift-3-incorrect-string-interpolation-with-implicitly-unwrapped-optionals – user2971617 Nov 24 '16 at 22:33
  • "File not found"? That's very different from "unexpected found nil". I was answering the unwrapping the optional question (and advising you to follow Apple's guidance to shift from paths to urls). If the file is not found, then that file is just not in your bundle. Is this file in the bundle or the documents folder? – Rob Nov 25 '16 at 05:35
  • Actually, when i do : let alertSound = URL(fileURLWithPath: Bundle.main.path(forResource: "tiesto" as String, ofType: "mp3")!). It's working perfectly. But when i m trying to input my variable; audioselectionne, , it doesn't work. Think it's an issue with uwrapping the optional ... – user2971617 Nov 25 '16 at 07:56
  • That's why I did `guard let resource = audioselectionne, let alertSound = ...`, that way, I'm unwrapping the `audioselectionne` optional first, and then using that in `Bundle.main.url(...)` call... – Rob Nov 25 '16 at 08:01
  • and i did. but same problem... In fact, i add print("not working...") between guard let ...... and return. And guess what happens when i m running the app... – user2971617 Nov 25 '16 at 08:04
0

I think the Bundle.main.path method returns an optional String. When that’s nil (because the resource was not found), force-unwrapping it causes your error. If you want to handle it correctly, you have to check for the nil:

guard let path = Bundle.main.path(…) else {
    // resource not found, handle error
}

// now `path` is guaranteed to be non-nil
let alertSound = URL(fileURLWithPath: path)
zoul
  • 102,279
  • 44
  • 260
  • 354
  • the problem is why with Swift3 , it doesn't find the file ? it's pretty weird – user2971617 Nov 24 '16 at 19:56
  • 1
    Maybe the path comes out different from your previous code in Swift 3, but the error you get _is_ caused by the force-unwrap (`!`). Don’t force-unwrap values unless you _have_ to. – zoul Nov 24 '16 at 19:58
  • Actually, if this is supposed to be in Bundle.main.path then you shouldn't handle the error but fix either the path or make sure the resource is there. – gnasher729 Nov 24 '16 at 20:40