Glitch in Swift Playgrounds Sphero Arcade “Back and Forth

In Swift Playgrounds Sphero Arcade 1, on the exercise “Back and Forth”, I’m having trouble setting the LED color. Can anyone help?

Here is the code I put into my project:


var currentPlayer = 1


func onCollision(data: CollisionData) {

        stopRoll()

        if currentPlayer == 1 {

            currentPlayer = 2

            setMainLed(color: UIColor.green) ERROR: Cannot find “UIColor” in scope

            volley(baseAngle: 180)

        } else {

            currentPlayer = 1

            

            volley(baseAngle: 0)

        }

        

    }
Our students were having the same issue and we could not find a solution.
Using .green rather than UIColor.green will get this to compile.

I’m not 100% sure what’s going on here but it seems that a previous version of Swift Playgrounds would implicitly import UIKit into this playground and hence the UIColor symbol would resolve. The latest version doesn’t do that, so using UIColor.green fails because Swift can’t find UIColor. However, when you use .green Swift is able to infer the correct type from the setMainLed(color:) context.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
Glitch in Swift Playgrounds Sphero Arcade “Back and Forth
 
 
Q