I have created some code in a playground, using modified code from the Apple developer website, which iterates over the items in a dictionary, and then changing the value of a variable based on whether the value of another variable is equal to the dictionary item. This works fine in a playground, but when I put this code into a project in Xcode, I get a Swift Compiler error telling me that the program 'Expected Declaration'. My code is as follows:
var x: Int = 6
var y: String = ""
let ColourDictionary = [0: "Black", 1: "Brown", 2: "Red", 3: "Orange", 4: "Yellow", 5: "Green", 6: "Blue", 7: "Violet", 8: "Grey", 9: "White"]
for (colourNumber, colourName) in ColourDictionary {
if x == colourNumber {
y = colourName
} else {
y = ""
}
}The variables x and y are linked to an input ('x') and an output ('y'). The dictionary and for loop are positioned in the middle of my program, in between variable x being defined and variable y defining the value of a UILabel.