0

I am creating an app that displays different colours as text strings on labels based on input from steppers. As part of this, I am trying to write a function in swift to set the string value for the labels, so that the code can be used multiple times for multiple labels and steppers. The function uses an inout parameter and a variable parameter:

func function(var Extra: Int, inout LabelName: String) {
    if Extra == 0 {
        LabelName = "Black"
    }
    if Extra == 1 {
        LabelName = "Brown"
    }
    if Extra == 2 {
        LabelName = "Red"
    }
    if Extra == 3 {
        LabelName = "Orange"
    }
    if Extra == 4 {
        LabelName = "Yellow"
    }
    if Extra == 5 {
        LabelName = "Green"
    }
    if Extra == 6 {
        LabelName = "Blue"
    }
    if Extra == 7 {
        LabelName = "Violet"
    }
    if Extra == 8 {
        LabelName = "Grey"
    }
    if Extra == 9 {
        LabelName = "White"
    }
    if Extra == -1 {
        LabelName = "Gold"
    }
    if Extra == -2 {
        LabelName = "Silver"
    }

}

When I try to call the function using

function(ValueLabel1Extra, ValueLabel1String)

I get a Swift Compiler Error saying 'Expected Declaration', which, from prior knowledge and some research, is an error where a function isn't declared properly if at all.

I'm not entirely sure what is not declared, but by trial and error it seems that the function itself is what isn't declared, but only when I call it. The problem persists even after several re-writes and edits to the code.

Thanks in advance!

1
  • 1
    You forgot a & before ValueLabel1String on function call. Except that, it works like a charm for me on a playground. Commented Aug 11, 2015 at 13:56

1 Answer 1

1

You need

function(ValueLabel1Extra, &ValueLabel1String)    

because it is not a local variable to the function that is being edited

Also consider switch statements like this, not essential, but generally saves space coding.

func function(var Extra: Int, inout LabelName: String) {

    switch(Extra){

        case 0:
            LabelName = "Black"
        case 1:
            LabelName = "Brown"
        case 2:
            LabelName = "Red"
        case 3:
            LabelName = "Orange"
        case 4:
            LabelName = "Yellow"
        case 5:
            LabelName = "Green"
        case 6:
            LabelName = "Blue"
        case 7:
            LabelName = "Violet"
        case 8:
            LabelName = "Grey"
        case 9:
            LabelName = "White"
        case -1:
            LabelName = "Gold"
        case -2:
            LabelName = "Silver"
        default:
            LabelName = "Black"
    }
}

var x: String = ""
function(4, &x)
println(x)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.