With NSBundle.mainBundle().pathForResource("Sound", ofType: "m4a")!
you are forcing the unwrapping of the Optional returned by NSBundle (note the !
at the end).
So if this resource "Sound.m4a" is unavailable, as it seems to be the case, the app will crash.
You need two things: make sure that the resource is in the bundle, and use safe unwrapping and error handling instead of force-unwrapping.
I strongly suggest you read the nice docs Apple has made available for us, all of this is explained in detail.
Also note that NSURL
doesn't throw so you don't have to use try
with it.
Update
Here's an example of what I'm talking about:
if let soundResourceURL = NSBundle.mainBundle().pathForResource("Sound", ofType: "m4a") {
let buttonAudioURL = NSURL(fileURLWithPath: soundResourceURL)
// use buttonAudioURL here
} else {
print("Unable to find Sound.m4a")
}