Justsoft
Justsoft

Reputation: 146

What does StringInterpolationSegment do? (swift)

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?

Upvotes: 2

Views: 851

Answers (3)

Bobj-C
Bobj-C

Reputation: 5426

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 

Upvotes: 0

Marc Fearby
Marc Fearby

Reputation: 1384

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!)

Upvotes: 0

Glorfindel
Glorfindel

Reputation: 22651

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)

Upvotes: 1

Related Questions