Swift How to display variables

0

I am finishing a small project to learn Swift but there is a small problem in putting more variables in output .

@IBOutlet weak var Nome: UITextField!
 @IBAction func CalcularIMC(_ sender: AnyObject) {

    imc = ((p * 200) / (a * a)) * 100 / 2
    var str = "";

    if imc < 16
    {
        str = NSString(format: "O seu IMC é %.2f, Peso Abaixo", imc) as String
        lblresultado.backgroundColor = UIColor.orange
}
 lblresultado.text = str;

Some parentheses are certainly missing but it was just for show. My problem is here:

(format: "O seu IMC é %.2f, Peso Abaixo", imc)

In this text I also want to show the Nome variable that is at the beginning of the code I showed.

    
asked by anonymous 21.12.2016 / 16:07

2 answers

1

Swift's native type is not NSString, but String. All initializers available for NSString also exist for String. If you want to put the text of your text field straight into your string you have to do it as follows:

str = String(format: "%@ o seu IMC é %.2f, Peso Abaixo", Nome.text!, imc)
    
23.12.2016 / 05:58
1

Look like this:

str = NSString(format: "%@ o seu IMC é %.2f, Peso Abaixo", nome, imc) as String
    
21.12.2016 / 16:25