简体   繁体   English

遇到:展开一个Optional值时意外发现nil,当我尝试使其变为可选值时,出现另一个错误

[英]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! 在您的情况下,您的farmtilenil ,但是您正在使用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.

相关问题 当我尝试通过AVAssetImageGenerator获取CGImage时,“在展开可选值时意外发现nil” - “unexpectedly found nil while unwrapping an Optional value” when I try to get a CGImage by AVAssetImageGenerator 尝试获取联系电话时,出现错误“致命错误:在展开可选值时意外发现nil” - I got an error “fatal error: unexpectedly found nil while unwrapping an Optional value” when I try to get a phone number in contact 在项目中添加了自定义字体,但是当我尝试使用它们时,出现“严重错误:在展开可选值时意外发现nil” - Added custom font to project but when I try and use them I get a “fatal error: unexpectedly found nil while unwrapping an Optional value” 当我实现Parse时,我得到“致命错误:在展开可选值时意外发现nil” - When I implement Parse I get “fatal error: unexpectedly found nil while unwrapping an Optional value” 当我尝试使用userDefaults保存为ui颜色时,Xcode给我以下错误:致命错误:在展开可选值时意外发现nil - When I try to save to ui colors with userDefaults, Xcode gives me the error : Fatal error: Unexpectedly found nil while unwrapping an Optional value 解包可选的 IBOutlet 时出现错误(线程 1:致命错误:解包可选值时意外发现 nil) - I have an error when unwrapping an optional IBOutlet (Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value) 迅速-播放声音时出现错误“严重错误:在展开可选值时意外发现nil” - Swift - While playing sound I get error “fatal error: unexpectedly found nil while unwrapping an Optional value” 为什么在尝试向iOS Swift中的服务器发出发布请求时出现此错误:在展开Optional值时意外发现nil? - Why I get this error while trying to make post request to a server in iOS Swift: unexpectedly found nil while unwrapping an Optional value? 运行我的应用程序时出现此错误。 线程1:致命错误:展开一个可选值时意外地找到零 - I get this error when i run my app. Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value 当我尝试访问它们时,IBOutlet变量返回nil。 错误:线程1:致命错误:展开一个可选值时意外发现nil - IBOutlet variables return nil when I try to access them. Error: Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM