Initialization error (Beginner here)

Just starting to use Xcode and swift for the first time. On my first app i am having trouble with this error.

‘initialization of variable 'lblBdrslt' was never used; consider replacing with assignment to '_' or removing it’ on line code 16

I’m not sure exactly how it is not being used because the label should be result of the equation. If someone could help me out with the code and understanding why it happened would be appreciated.


import UIKit
class ViewController: UIViewController {
    @IBOutlet weak var txtBDwt: UITextField!
    @IBOutlet weak var lblBdrslt: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        /
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        /
    }
    @IBAction func btnCal(sender: AnyObject) {
        let bdWT = Double(txtBDwt.text!)
        let bdFormCal: Double = 0.06232
        var lblBdrslt = bdWT! * bdFormCal
       
    }
}
Accepted Answer

lblBdrslt is of type UILabel. And, as it's marked with @IBOutlet, I'm assuming you've made a connection between that instance variable and the view controller in your storyboard.


Inside of your btnCal function, you are declaring another local variable named 'lblBdrslt' and from the right-hand side of the =, it will have a type of Double. You're getting the error since that result isn't being used at all.


What you should have done is to first convert the result of your calculation to a string. Then, assign that string to your label's text property:


let theResult = bdWT! * bdFormCal
lblBdrslt.text = "\(theResult)"


As a next step, you'll want to look at using NumberFormatter in order to convert user input to a Double and then the resulting Double back to a string. That will let you work with the world's formats.

Can you confirm you've wired up that label/outlet via IB?

Thanks rsharp, that was exactly what I was looking for!

Initialization error (Beginner here)
 
 
Q