[英]Running into: Unexpectedly found nil while unwrapping an Optional value, when I try to make it optional, I get another error
I get this error when trying to make farmTile
optional: Value of optional type Bool?
尝试使
farmTile
可选项时出现此错误:可选类型Bool?
值Bool?
not unwrapped;
不
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")
}
}
farmTile?.contains(touchLocation)
returns an optional Bool. farmTile?.contains(touchLocation)
返回可选的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")
}
}
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
. 出现此错误的原因:
Unexpectedly found nil while unwrapping an Optional value
是因为您为某个不应该为nil
的东西指定了nil
。 In your case, your farmtile
is nil
, but you are using SKSpriteNode!
在您的情况下,您的
farmtile
为nil
,但是您正在使用SKSpriteNode!
which in lay mans terms means at the time of using this variable, it better not be nil
. 用通俗的话来说,这意味着在使用此变量时,最好不要为
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
. 设置Farmtile的唯一一点是在此行
farmTile = SKSpriteNode(imageNamed: "farmtile.png")
,这意味着您的图像文件有问题,因为SKSpriteNode
返回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 '?'?
这是您遇到第二个错误的地方:
optional: Value of optional type Bool? not unwrapped; did you mean to use '!' or '?'?
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. 到您的
farmTile
以为它可以解决您的问题,如果不能解决。
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. 在这一点上,我们对其进行了设计,以使
farmTile
必须存在(基本上是通过确保100%调用createFarmTile
函数),因此没有理由添加其他拆包,因此请删除问号。
//Inside the touch begins function
if farmTile.contains(touchLocation){
print("test")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.