Superllanboy Superllanboy - 3 years ago 234
Swift Question

Expression Type UIImage is ambiguous without more context

The following code produces the Error shown in the title of this question on this line : for (index, image) in (backgroundImages) {

can someone advise how to silence this error

init!(backgroundImages:[UIImage], size:CGSize, scrollingDirection:ScrollingDirection, startingSpeed:CGFloat, speedDecreaseFactor:CGFloat) {
self.backgrounds = []
self.clonedBackgrounds = []
self.speeds = []
self.numberOfBackgrounds = (backgroundImages.count)
self.scrollingDirection = scrollingDirection
super.init(texture: nil, color:UIColor.clear, size: size)

let zPos = 1.0 / CGFloat(numberOfBackgrounds)
var currentSpeed = startingSpeed
self.position = CGPoint(x:self.size.width/2, y:self.size.height/2)
self.zPosition = -100

for (index, image) in (backgroundImages) {
let background = SKSpriteNode(texture: SKTexture(CGImage: image.CGImage), size:size)

background.zPosition = self.zPosition - (zPos + (zPos * CGFloat(index)))
background.position = CGPoint(x:0,y:0)
let clonedBackground = background.copy() as! SKSpriteNode
var clonedBackgroundX = background.position.x
var clonedBackgroundY = background.position.y

switch (scrollingDirection) {
case .Right:
clonedBackgroundX = -background.size.width
case .Left:
clonedBackgroundX = background.size.width

Answer Source

You probably meant for (index, image) in backgroundImages.enumerated()

The compiler can't figure out how to deconstruct elements of backgroundImages to (index, image).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download