Swift - While playing sound I get error "fatal error: unexpectedly found nil while unwrapping an Optional value"

Govind Rakholiya

I am using Xcode 7.0.1 Swift 2 iOS 9. While playing sound I get this error:

"fatal error: unexpectedly found nil while unwrapping an Optional value"

Error Screenshot

and this is my code:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    playSound(enumerator![indexPath.item] )
}

func playSound(soundName: String)
{
    let coinSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(soundName, ofType: "m4a")!)
    do{
        let audioPlayer = try AVAudioPlayer(contentsOfURL:coinSound)
        audioPlayer.prepareToPlay()
        audioPlayer.play()
    }catch {
        print("Error getting the audio file")
    }
}
GoodSp33d

NSBundle pathForResource(name: String?, ofType ext: String?) -> String? returns an optional, and you are force unwrapping. Either your path is wrong or your resource is not there.

And looking at the image sound name has .m4a extension. If you want to provide extension yourself, you can skip ofType and pass nil or separate the extension from the name of the resource and send both parameters.

To be safe, you should always check for optionals when you are not sure if it has value or not

let pathComponents = soundName.componentsSeparatedByString(".")
if let filePath = NSBundle.mainBundle().pathForResource(pathComponents[0], ofType: pathComponents[1]) {
    let coinSound = NSURL(fileURLWithPath: filePath)
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

fatal error: unexpectedly found nil while unwrapping an Optional value, Get json error swift

From Dev

fatal error: unexpectedly found nil while unwrapping an Optional value, Get json error swift

From Dev

How can I fix "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value" in Swift

From Dev

fatal error: unexpectedly found nil while unwrapping an Optional value swift

From Dev

swift fatal error: unexpectedly found nil while unwrapping an Optional value

From Dev

fatal error: unexpectedly found nil while unwrapping an Optional value swift

From Dev

fatal error: unexpectedly found nil while unwrapping an Optional value (Swift)

From Dev

Swift:fatal error: unexpectedly found nil while unwrapping an Optional value In dispatch_async dispatch_get_global_queue

From Dev

when coding, I get Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value any solutions

From Dev

fatal error: unexpectedly found nil while unwrapping an Optional value swift error

From Dev

Swift Error - fatal error: unexpectedly found nil while unwrapping an Optional value

From Dev

fatal error: unexpectedly found nil while unwrapping an Optional value error Swift

From Dev

fatal error: unexpectedly found nil while unwrapping an Optional value: Swift, Core Data

From Dev

CLLocationManager fatal error: unexpectedly found nil while unwrapping an Optional value Swift

From Dev

Swift fatal error: unexpectedly found nil while unwrapping an Optional value (lldb) in Tableview

From Dev

fatal error: unexpectedly found nil while unwrapping an Optional value in Swift when tried to parse JSON

From Dev

swift - UICollectionView issue "fatal error: unexpectedly found nil while unwrapping an Optional value"

From Dev

Swift Alamofire - fatal error: unexpectedly found nil while unwrapping an Optional value - youtube api: PlaylistItems: list

From Dev

Swift: label text --> "fatal error: unexpectedly found nil while unwrapping an Optional value"

From Dev

Xcode - Swift - NSURL : "fatal error: unexpectedly found nil while unwrapping an Optional value"

From Dev

parsing JSON in swift throws 'fatal error: unexpectedly found nil while unwrapping an Optional value'

From Dev

Swift: fatal error: unexpectedly found nil while unwrapping an Optional value when initializing UIlabel value

From Dev

swift: fatal error: unexpectedly found nil while unwrapping an Optional value (lldb), Thread 1

From Dev

fatal error: unexpectedly found nil while unwrapping an Optional value when using AudioPlayer in Swift 2

From Dev

Swift Generic Variables fatal error: unexpectedly found nil while unwrapping an Optional value

From Dev

swift fatal error: unexpectedly found nil while unwrapping an Optional value(lldb)from email -in my code?

From Dev

swift searchbar and tableview - fatal error: unexpectedly found nil while unwrapping an optional value

From Dev

swift nsurlconnection fatal error: unexpectedly found nil while unwrapping an Optional value

From Dev

Swift: Fatal error: unexpectedly found nil while unwrapping an Optional value (SpriteKit)

Related Related

  1. 1

    fatal error: unexpectedly found nil while unwrapping an Optional value, Get json error swift

  2. 2

    fatal error: unexpectedly found nil while unwrapping an Optional value, Get json error swift

  3. 3

    How can I fix "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value" in Swift

  4. 4

    fatal error: unexpectedly found nil while unwrapping an Optional value swift

  5. 5

    swift fatal error: unexpectedly found nil while unwrapping an Optional value

  6. 6

    fatal error: unexpectedly found nil while unwrapping an Optional value swift

  7. 7

    fatal error: unexpectedly found nil while unwrapping an Optional value (Swift)

  8. 8

    Swift:fatal error: unexpectedly found nil while unwrapping an Optional value In dispatch_async dispatch_get_global_queue

  9. 9

    when coding, I get Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value any solutions

  10. 10

    fatal error: unexpectedly found nil while unwrapping an Optional value swift error

  11. 11

    Swift Error - fatal error: unexpectedly found nil while unwrapping an Optional value

  12. 12

    fatal error: unexpectedly found nil while unwrapping an Optional value error Swift

  13. 13

    fatal error: unexpectedly found nil while unwrapping an Optional value: Swift, Core Data

  14. 14

    CLLocationManager fatal error: unexpectedly found nil while unwrapping an Optional value Swift

  15. 15

    Swift fatal error: unexpectedly found nil while unwrapping an Optional value (lldb) in Tableview

  16. 16

    fatal error: unexpectedly found nil while unwrapping an Optional value in Swift when tried to parse JSON

  17. 17

    swift - UICollectionView issue "fatal error: unexpectedly found nil while unwrapping an Optional value"

  18. 18

    Swift Alamofire - fatal error: unexpectedly found nil while unwrapping an Optional value - youtube api: PlaylistItems: list

  19. 19

    Swift: label text --> "fatal error: unexpectedly found nil while unwrapping an Optional value"

  20. 20

    Xcode - Swift - NSURL : "fatal error: unexpectedly found nil while unwrapping an Optional value"

  21. 21

    parsing JSON in swift throws 'fatal error: unexpectedly found nil while unwrapping an Optional value'

  22. 22

    Swift: fatal error: unexpectedly found nil while unwrapping an Optional value when initializing UIlabel value

  23. 23

    swift: fatal error: unexpectedly found nil while unwrapping an Optional value (lldb), Thread 1

  24. 24

    fatal error: unexpectedly found nil while unwrapping an Optional value when using AudioPlayer in Swift 2

  25. 25

    Swift Generic Variables fatal error: unexpectedly found nil while unwrapping an Optional value

  26. 26

    swift fatal error: unexpectedly found nil while unwrapping an Optional value(lldb)from email -in my code?

  27. 27

    swift searchbar and tableview - fatal error: unexpectedly found nil while unwrapping an optional value

  28. 28

    swift nsurlconnection fatal error: unexpectedly found nil while unwrapping an Optional value

  29. 29

    Swift: Fatal error: unexpectedly found nil while unwrapping an Optional value (SpriteKit)

HotTag

Archive