-1

The following code works fine with Xcode 7.2.1. After I upgraded to 7.3 I get a crash with: "unexpectedly found nil while unwrapping an Optional value"

var backgroundMusic: SKAudioNode!

func playBackgroundMusic(name: String) {
  var delay = 0.0
  if backgroundMusic != nil {
    backgroundMusic.removeFromParent()
  } else {
delay = 0.1 }
  runAction(SKAction.waitForDuration(delay)) {
    self.backgroundMusic = SKAudioNode(fileNamed: name)
    self.backgroundMusic.autoplayLooped = true
    self.addChild(self.backgroundMusic)
} }

didMoveToView(view: SKView) {
...
playBackgroundMusic("Music.mp3")
}
  • I also tried cleaning the project, deleting all the Xcode Developer derived data, and verifying that the "Music.mp3" is being built into the main bundle.
  • Going back to Xcode 7.2.1 and building works fine with the same code.

Been scratching my head on this one most of the day. Is there a better way to do this?

nhgrif
  • 61,578
  • 25
  • 134
  • 173
keydogg
  • 183
  • 1
  • 6
  • 1
    Which line is throwing an error – Dan Levy Mar 29 '16 at 00:13
  • perhaps it is no longer finding your audio file? – Knight0fDragon Mar 29 '16 at 01:19
  • I agree @Daniel Ormeño answer, since you didn't find which line resulted in this error. Even though this fatal error in swift never give a break point automatically, you can still set your own break point to locate which unwrapping is the culprit. – Christopher Mar 29 '16 at 04:46
  • 1
    Thanks, it's crashing at: self.backgroundMusic.autoplayLooped = true – keydogg Mar 29 '16 at 16:25
  • @Knight0fDragon can you vote to reopen this? This problem seems to be specific to SKAudioNode, and not necessarily related to the optional question it's been closed as similar to. – Confused Nov 04 '16 at 16:10

2 Answers2

0

Try changing your variable declaration to an optional.

var backgroundMusic: SKAudioNode?

Then unwrap it as so:

if let bm = backgroundMusic {
  bm.removeFromParent()
} else {
  delay = 0.1 }
  runAction(SKAction.waitForDuration(delay)) {
  self.backgroundMusic = SKAudioNode(fileNamed: name)
  self.backgroundMusic.autoplayLooped = true
  self.addChild(self.backgroundMusic)
}
Daniel Ormeño
  • 2,743
  • 2
  • 25
  • 30
  • Thanks I tried that but it still crashes at the below line. Maybe this is an Xcode issue and it's not finding my audio file? self.backgroundMusic!.autoplayLooped = true – keydogg Mar 29 '16 at 16:32
  • 1
    No worries, I doubt it would be an Xcode issue. Can you update the code in the question with your latest? in the line that you refer to "self.backgroundMusic!.autoplayLooped = true" you are forcing the unwrapping of the optional by doing self.backgroundMusic!, which is where your app is finding the nil. Use "if let" to unwrap your optional after instantiating it and attach the debugger to see what the constructor of the SKAudioNode is doing, it might be returning nil, check if the file you are referencing its target checked. Cheers – Daniel Ormeño Mar 30 '16 at 00:17
  • I am having the same issue in XCode 7.3.1 after updating it from XCode 7.2. – Nitesh Borad May 19 '16 at 12:47
0

try this.With temp constant

var backgroundMusic: SKAudioNode!

func playBackgroundMusic(name: String) {
  var delay = 0.0
  if backgroundMusic != nil {
    backgroundMusic.removeFromParent()
  } else {
  delay = 0.1
  runAction(SKAction.waitForDuration(delay)) {
    let temp = SKAudioNode(fileNamed: name)
    self.backgroundMusic = temp
    self.backgroundMusic.autoplayLooped = true
    self.addChild(self.backgroundMusic)
 } 
}

didMoveToView(view: SKView) {
...
playBackgroundMusic("Music.mp3")
}