1

I am trying to access "numOne" right before the return statement and I can't seem to find why it is not working. Everything was fine accessing "numTwo". Is the reason because "numOne" is inside a range?

func checknumber() -> String {
    var numTwo = 0
    var range = 0...b
    for numOne in range {
        numTwo = b-numOne
        if let result = isCommon(numOne: numOne, numTwo: numTwo) {
            println("Success - \(numOne) and \(numTwo) Work")

            break
        } else {
            println("Failure - \(numOne) and \(numTwo) Does NOT Work")
        }
        var numOneFinal = numOne
    }

    var numberTwo = "\(numTwo)"
    var numberOne = "\(numOneFinal)"

    return numberTwo
}

If anyone could point me in the right direction that would be greatly appreciated.

2 Answers 2

2

numOne is scoped by the for statement in which it is defined.

To gain access to the last value of numOne after the loop, add a new variable, say numOneFinal, at the same level as numTwo, then assign numOne's value to it within the for loop.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank your for your reply. I did what you said but I am getting an "Use of unresolved identifier 'numOneFinal' error. What am I doing wrong? I have update my code in the question to show you what I am doing
You need to declare the new variable, same as you have numTwo above.
1

You can't access numOne outside of the for loop, because it's out of the scope. You can only access numOne within the loop

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.