2

I continued on my math question project, and it could now generate questions, so I need to try to use a global variable to use my answer's in generating the answer itself and putting the answer randomly into one of 4 choices. I decided to put a secret label outside of my view that shows the answer. Here's my code:

    //important stuff
@IBOutlet var secretAnsarrrrr: UILabel!
//useless code
        //declare the value of secretAnsarrrr
        secretAnsarrrrr.text = String(answer)
        numA.text = String(Int(randomNumber))
        numB.text = String(Int(randomNumber2))
    }
    generateQuestion()
}
var optionAnswer:UInt32 = arc4random_uniform(4)
@IBAction func answerA(sender: UIButton) {
    var otherAns:String = String(secretAnsarrrrr) //error
    if optionAnswer == 0 {
        optionA.text = secretAnsarrrrr.text
    }

Doing so gives me the error of 'Missing argument label: StringInterpolationSegment in call'. It tells me to place it behind 'secretAnsarrrrr' in the parentheses. What does stringInterpolationSegment do and how do I fix this?

3 Answers 3

1

secretAnsarrrrr is not a string itself - it is a UILabel which also contains information like color and font. You need to tell Swift to use its text property:

 var otherAns:String = String(secretAnsarrrrr.text)
Sign up to request clarification or add additional context in comments.

Comments

0

I just encountered this error, and after some head scratching, I realised that I needed to unwrap an optional integer which I was trying to parse with String(...). As soon as I added the exclamation mark, the problem was solved:

var num: Int? = 1
var str = String(num!)

Comments

0

it is Better to write:

var num: Int? = 1
var str = num.map { String($0) } // if num == nil then str = nil else str = string representation 

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.