P.Dane P.Dane - 5 months ago 32
Swift Question

Minor Syntax issue regarding Swift 3.0 and a for loop

I recently just converted my code to the Swift 3.0 syntax that came with the Xcode 8 beta. I ran into several lines of code that I needed to change in order for the code to work with the latest syntax. I was able to correct all lines of code except for an error that I get regarding a for loop that I use to allow my background image to loop continuously.

The exact error message that I get is: Ambiguous reference to member '..<'

for i:CGFloat in 0 ..< 3 {

let background = SKSpriteNode(texture: backgroundTexture)
background.position = CGPoint(x: backgroundTexture.size().width/2 + (backgroundTexture.size().width * i), y: self.frame.midY)
background.size.height = self.frame.height
background.run(movingAndReplacingBackground)
self.addChild(background)

}

Answer

Don't use a floating point type as loop index

for i in 0 ..< 3 {
    let background = SKSpriteNode(texture: backgroundTexture)
    background.position = CGPoint(x: backgroundTexture.size().width/2 + (backgroundTexture.size().width * CGFloat(i)), y: self.frame.midY)
    background.size.height = self.frame.height
    background.run(movingAndReplacingBackground)
    self.addChild(background)
}