1

working on this code for few hours without success,please advice:

i am building an ios 9 spritekit game.this method:

func createFiringParticles(location:CGPoint, force:CGVector){

    let fireEmitter = SKEmitterNode(fileNamed: "FireParticles")

    fireEmitter!.position = location
    fireEmitter!.name = "fireEmitter"
    fireEmitter!.zPosition = 1
    fireEmitter!.targetNode = self
    fireEmitter!.numParticlesToEmit = 50

    fireEmitter!.xAcceleration = force.dx
    fireEmitter!.yAcceleration = -force.dy

    self.addChild(fireEmitter!)

}

gives this error: fatal error: unexpectedly found nil while unwrapping an Optional value

the error is not initiated at first (so it works for few minutes till it crash).i tried to unwrap the SKEmitterNode with NSKeyedUnarchiver:

    var unWrappedFireEmitter = NSBundle.mainBundle().pathForResource("FireParticles", ofType: "sks")!
    var fireEmitter = NSKeyedUnarchiver.unarchiveObjectWithFile(unWrappedFireEmitter) as! SKEmitterNode
    fireEmitter.position = location
    fireEmitter.name = "fireEmitter"
    ..........
    self.addChild(fireEmitter)

without any success. how can you unwrap SKEmitterNode? is it something in sks file? enter image description here many thanks for your help!!!!

ohad
  • 127
  • 2
  • 6
  • You are force-unwrapping (`!`). If the optional variable is `nil`, that won't magically give you a valid instance. You need to check that it is not `nil` before force-unwrapping. This is very basic stuff, I think you should read the Swift book at least: https://itunes.apple.com/jp/book/swift-programming-language/id881256329?mt=11 – Nicolas Miari Sep 17 '15 at 09:44
  • In your particular case, `NSBundle...` might very well return `nil`: e.g., you specified the path of a non-existent file to read from. – Nicolas Miari Sep 17 '15 at 09:45

1 Answers1

1

NSKeyedUnarchiver.unarchiveObjectWithFile() is throwing an exception because it expects a non-nil string (type: String) as the argument, and you are passing an optional string (i.e., type String?) and force unwrapping it, when it just happens to contain nil ("no value at all") instead of a valid value.

This seems to be happening because the earlier call to NSBundle.mainBundle().pathForResource(_:, ofType:) must be failing (the path can not be found among your resources. i.e, said file does not exist).

In your particular case, this code should work:

let optionalFireEmitter = NSBundle.mainBundle().pathForResource("FireParticles", ofType: "sks")

if let unWrappedFireEmitter = optionalFireEmitter {
    // you can use your unWrappedFireEmitter, no need to append "!"
    // (it is already unwrapped)

    // (...your code here...)
}
else{
    // NSBundle.mainBundle().pathForResource() returned nil: Can't use.
    // Make sure the file name and extension you specified are correct
    // and that the resource file is included in your binary.
}
Nicolas Miari
  • 16,006
  • 8
  • 81
  • 189
  • thanks,i have already tried this way but this leads to more errors (in a 1000' lines code) i need to find a way to give a non nil value to the unwrappedFireEmitter if nil.is it possible? – ohad Sep 17 '15 at 09:50
  • What errors exactly? It is a very common piece of code. – Nicolas Miari Sep 17 '15 at 09:51
  • 2
    If your `pathForResource` returns `nil`, there's nothing you can do. You must fix the name or extension that you passed, and make sure your project has the specified resource. – Nicolas Miari Sep 17 '15 at 09:52