Initializer for conditional binding must have Optional type, not 'SKTileDefinition'

I have this error on my code, I think something changed on Swift… cause I use exactly the same code on book. Can anyone help me?

guard let tile = tile(in: bugsMap, at: (column, row))
else {continue}

Complete func

func createBugs() {
guard let bugsMap = childNode(withName: “bugs”)
as? SKTileMapNode else {return}

//1 varre as linhas e colunas do tile map node
for row in 0..<bugsMap.numberOfRows {
  for column in 0..<bugsMap.numberOfColumns {
    //2
    guard let tile = tile(in: bugsMap, at: (column, row))
      else {continue}
    
    //3
    let bug = Bug()
    bug.position = bugsMap.centerOfTile(atColumn: column, row: row)
    bugsNode.addChild(bug)
    
  }
}
//4

bugsNode.name = "Bugs"
addChild(bugsNode)

bugsMap.removeFromParent()

}

What error are you getting? That code looks correct. I ran the final project and everything seemed to work fine.

Compare your code with the provided final project.

I was getting this “Initializer for conditional binding must have Optional type, not ‘SKTileDefinition’” error.

But I found it, my func tile() → SKTileDefinition was missing a “?” on the SKTileDefinition.

Then I got a thread error on this func, checked everything again and when I retype that tile function it worked perfectly! :thinking::sweat_smile:

Btw, thank you for answering and helping! :wink:

1 Like

This topic was automatically closed after 166 days. New replies are no longer allowed.