//
// ViewController.swift
// SimpleTemperatureConverter
//
// Created by Kyoung Shin Park on 10/2/16.
// Copyright © 2016 Kyoung Shin Park. All rights reserved.
//
import UIKit
extension String {
func toDouble() -> Double {
if let unwrappedNum = Double(self) {
return unwrappedNum
}
else {
print(“Error converting \”” + self + “\” to Double”)
return 0.0
}
}
func toInt() -> Int {
if let unwrappedNum = Int(self) {
return unwrappedNum
}
else {
print(“Error converting \”” + self + “\” to Int”)
return 0
}
}
}
class ViewController: UIViewController {
//var fahrenheit : Double = 0.0
//var celsius : Double = 0.0
@IBOutlet weak var fahrenheitTextfield: UITextField!
@IBOutlet weak var celsiusTextfield: UITextField!
@IBAction func convertFahrenheitToCelsius(sender: AnyObject) {
if let _ = sender as? UIButton {
let fahrenheit = fahrenheitTextfield.text!.toDouble()
let converter = FahrenheitToCelsius(temperature: fahrenheit)
celsius = converter.celsius
//celsiusTextfield.text = “\(celsius)”
celsiusTextfield.text = String.localizedStringWithFormat(“%.2f”, celsius)
}
}
@IBAction func convertCelsiusToFahrenheit(sender: AnyObject) {
if let _ = sender as? UIButton {
celsius = celsiusTextfield.text!.toDouble()
let converter = CelsiusToFahrenheit(temperature: celsius)
fahrenheit = converter.fahrenheit
//fahrenheitTextfield.text = “\(fahrenheit)”
fahrenheitTextfield.text = String.localizedStringWithFormat(“%.2f”, fahrenheit)
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
fahrenheit = 50.0
fahrenheitTextfield.text = “\(fahrenheit)”
celsiusTextfield.text = “”
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}