2015-11-19 12 views
6

In Swift come faccio a convertire int in stringa e in retromarcia e visualizzare i risultati?

Il programma è supporre di modificare F TO C e invertire. Con lo switch cambia da on a off e on suppone che sia da C a f e off sia da F a c e inserendo il # sotto nel campo di testo. Quando si fa clic sul pulsante di invio, viene preso tutto ciò che nel campo di testo lo trasferisce a un preforme dell'algoritmo e quindi lo visualizza nel campo di testo.

Credo che la conversione funzioni correttamente ma non visualizzerà il risultato effettivo. O il modo in cui la sua conversione è sbagliata.

@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) 
    } 

} 
+1

Il primo problema che vedo subito è che si sta convertendo da F a C indipendentemente dalla posizione dell'interruttore. C'era di più per il tuo problema? – Tyrelidrel

+0

Bene, quello che vedo è che la funzione di conversione è sbagliata in entrambi i casi. Dovrebbe essere C = (F - 32) * 5/9 e F = (C * 9/5) + 32 –

risposta

4

Sto utilizzando una versione precedente di XCode (6.4), quindi il mio codice sarà leggermente diverso dal tuo. Da quello che ho capito, la tua funzione buttonClicked dovrebbe prendere l'argomento di AnyObject instend di UIButton. Inoltre non chiami affatto la funzione stateChanged nel tuo codice. Il seguente codice dovrebbe aiutare a raggiungere ciò che stai cercando di fare.

@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) 
    } 
} 
Problemi correlati