In Swift, how to convert int to string and vice versa and display result?

The program should change F TO C and vice versa. With the switch, it changes from on and off, which means it should be from C to f and off from F to c and enter # under the text box. When you click the Submit button, it accepts the fact that in a text field it passes it to the algorithm prefix, and then it displays it in the text field.

I believe that the conversion is going right, but does not display the actual result. Or how his conversion is wrong.

@IBOutlet weak var buttonClicked: UIButton! @IBOutlet weak var mySwitch: UISwitch! @IBOutlet weak var myTextField: UITextField! @IBOutlet weak var User: UITextField! func stateChanged(switchState: UISwitch) { if switchState.on { myTextField.text = "Convert to Celius" } else { myTextField.text = "Convert to Farheniet" } } @IBAction func buttonClicked(sender: UIButton) { if mySwitch.on { var a:Double? = Double(User.text!) a = a! * 9.5 + 32 User.text=String(a) mySwitch.setOn(false, animated:true) } else { var a:Double? = Double(User.text!) a = a! * 9.5 + 32 User.text=String(a) mySwitch.setOn(true, animated:true) } } 
+6
source share
1 answer

I am using an older version of Xcode (6.4), so my code will be slightly different from yours. From what I understand, your buttonClicked function should take an AnyObject instend UIButton argument. Also, you do not call the stateChanged function in your code at all. The following code should help you achieve what you are trying to do.

 @IBOutlet weak var mySwitch: UISwitch! @IBOutlet weak var myTextField: UITextField! @IBOutlet weak var User: UITextField! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. // sets the textfield to the intended conversion on load. if mySwitch.on { myTextField.text = "Convert to Celius" } else { myTextField.text = "Convert to Farheniet" } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // changes the myTextFiled text to the intended conversion when the switch is manually switched on or off @IBAction func switched(sender: AnyObject) { if mySwitch.on { myTextField.text = "Convert to Celsius" } else { myTextField.text = "Convert to Fahrenheit" } } // changes the myTextField text to intended reverse conversion after the buttonClicked func is completed. func stateChanged(switchState: UISwitch) { if switchState.on { myTextField.text = "Convert to Celsius" } else { myTextField.text = "Convert to Fahrenheit" } } // do the intended conversion(old version of XCode 6.4) @IBAction func buttonClicked(sender: AnyObject) { if mySwitch.on { var a = (User.text! as NSString).doubleValue a = (a-32)*(5/9) User.text="\(a)" mySwitch.setOn(false, animated:true) stateChanged(mySwitch) } else { var a = (User.text! as NSString).doubleValue a = a * (9/5) + 32 User.text="\(a)" mySwitch.setOn(true, animated:true) stateChanged(mySwitch) } } 
+4
source

All Articles