Reputation: 33
I get this error when trying to make farmTile
optional: Value of optional type Bool?
not unwrapped;
did you mean to use '!' or '?'?
It was working fine an hour ago.
var farmTile:SKSpriteNode!
func createFarmTile(){
farmTile = SKSpriteNode(imageNamed: "farmtile.png")
farmTile?.size = CGSize(width: 185, height: 195)
farmTile?.position = CGPoint(x: -95, y: 150)
self.addChild(farmTile!)
farmTile?.name = "farmTile"
farmTile.zPosition = 1
if farmTile?.contains(touchLocation){
print("test")
}
}
Upvotes: 0
Views: 96
Reputation: 16827
The reason you are getting this error: Unexpectedly found nil while unwrapping an Optional value
is because you have a nil
for something that should not be nil
. In your case, your farmtile
is nil
, but you are using SKSpriteNode!
which in lay mans terms means at the time of using this variable, it better not be nil
.
The only point you set farmtile is on this line farmTile = SKSpriteNode(imageNamed: "farmtile.png")
, which means you have an issue with your image file because SKSpriteNode
is coming back nil
. I would recommend that you check to make sure you have your image file correctly set up within your assets, it is named correctly, and that it is getting bundled into your binary. If I knew how you were setting up your assets, I could further assist here.
After you solve this problem, you have another problem ahead of you. (I assume this is going to be in a different function)
if farmTile?.contains(touchLocation){
print("test")
}
This is where you get your second error: optional: Value of optional type Bool? not unwrapped; did you mean to use '!' or '?'?
You are getting this error at compile time instead of run time because you added a ? to your farmTile
thinking it will fix your problem, when it doesn't.
Now at this point you may as well correctly be handling your optionals.
You can use a guard statement to verify that the sprite is being created, and pass on a more meaningful message to yourself so that you know where the problem is in the future.
var farmTile:SKSpriteNode!
func createFarmTile(){
guard let farmTile = SKSpriteNode(imageNamed: "farmtile.png") else { //insert your error handling here}
farmTile.size = CGSize(width: 185, height: 195)
farmTile.position = CGPoint(x: -95, y: 150)
farmTile.name = "farmTile"
farmTile.zPosition = 1
self.farmTile = farmTile
self.addChild(farmTile)
}
At this point we designed it so that farmTile
has to exist (Basically by ensuring 100% that the createFarmTile
function got called), so there is no reason to add an additional unwrap, so take out the question mark.
//Inside the touch begins function
if farmTile.contains(touchLocation){
print("test")
}
Upvotes: 1
Reputation: 86651
farmTile?.contains(touchLocation)
returns an optional Bool. You should handle the optional properly. Do something like this...
var farmTile: SKSpriteNode? // optional
func createFarmTile(){
guard let farmTile = SKSpriteNode(imageNamed: "farmtile.png")
else { /* do what you need to do if it is missing */ ; return }
farmTile.size = CGSize(width: 185, height: 195)
farmTile.position = CGPoint(x: -95, y: 150)
self.addChild(farmTile)
farmTile.name = "farmTile"
farmTile.zPosition = 1
self.farmTile = farmTile
if farmTile.contains(touchLocation){
print("test")
}
}
Upvotes: 2