'Expected declaration' when iterating over a dictionary

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.

Aren't you doing something like this?

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        //
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        //
    }
    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  = ""
        }
    }
}

In this case, for-statement is put at where class member declarations should be. Excutable statement, like for-statement needs to be inside a method or a closure.

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        //
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        //
    }
    @IBOutlet var label: UILabel! //This needs to be a member of the class.
    func aMethod() {
        //Following code needs to be inside a method.
        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  = ""
            }
        }
    }
}

Note: x, y, ColourDictionary can be class members, if you need them so. Only for-statement needs to be inside a method.


EDIT: Note added.

Thank you very much, his solved my initial problem. I now, however, get an 'Expected declaration' error when I try to call the function immediately after defining it.

If you have written something like this:

    aMethod() //<-This is an executable statement.

    func aMethod() {
        //Following code needs to be inside a method.
        var x: Int = 6
        var y: String = ""
        //...

then I need to say the same thing again:

Excutable statement needs to be inside a method or a closure.


If you mainly intend to study Swift language, better try a new project: OS X>Application>Command Line Tool.

In `main.swift`, (it's the only place you can write excutable statements other than REPL & Playground) you can write top-level executable statements as in the Playground.


If you want to learn iOS programming alongside studying Swift language, you'd better read basic documentations:

The Swift Programming Language

View Controller Programming Guide for iOS

UIViewController Class Reference

Start Developing iOS Apps (Swift)

...


So, if you want to test your code embedded in an actual app codes, viewDidLoad would be a good place, as it exists in the simplest project template, and invoked every time the view loaded:

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        //
        aMethod()
    }

    //...
'Expected declaration' when iterating over a dictionary
 
 
Q