swift
1`````````import UIKit 2 3class ViewController: UIViewController,UITextFieldDelegate { 4 @IBOutlet weak var inputText: UITextField? 5 @IBOutlet weak var outputText: UILabel! 6 @IBOutlet weak var outputcurrency: UILabel! 7 8 var money = Money() 9 var currency: String = "YEN" 10 11 @IBAction func doConversionYEN(_ sender: UIButton) { 12 currency = "YEN" 13 outputcurrency.text = currency 14 } 15 @IBAction func doConversionEUR(_ sender: UIButton) { 16 currency = "EUR" 17 outputcurrency.text = currency 18 } 19 20 func fun(textField: UITextField)->Bool{ 21 var convertAmount:Double = 0 22 23 textField.resignFirstResponder() 24 25 if let amount = Int((textField.text)!){ 26 money.setAmount(inputMoney: Double(amount)) 27 money.setCurrency(cur: "USD") 28 convertAmount = money.convert(cur: currency).getAmount() 29 if currency == "YEN"{ 30 outputText.text = String(format: "%.0f",convertAmount) 31 } 32 else if currency == "EUR"{ 33 outputText.text = String(format: "%.2f",convertAmount) 34 } 35 outputcurrency.text = currency 36 } 37 else{ 38 outputText.text = "エラーでっせ" 39 } 40 return true 41 } 42 43 override func viewDidLoad() { 44 super.viewDidLoad() 45 // Do any additional setup after loading the view, typically from a nib. 46 inputText?.delegate = self 47 money.setCurrency(cur: "USD") 48 currency = "YEN" 49 outputcurrency.text = currency 50 } 51 52 override func didReceiveMemoryWarning() { 53 super.didReceiveMemoryWarning() 54 // Dispose of any resources that can be recreated. 55 } 56} 57 class Money{ 58 var amount: Double = 0 59 var currency: String = "" 60 var rates:Dictionary<String,Double> = ["USDYEN":120,"USDEUR":1.2] 61 62 func setAmount(inputMoney: Double){ 63 amount = inputMoney 64 } 65 66 func setCurrency(cur: String){ 67 currency = cur 68 } 69 70 func convert(cur: String)->Money{ 71 let money = Money() 72 money.setAmount(inputMoney: amount*rates[currency+cur]!) 73 money.setCurrency(cur: currency) 74 return money 75 } 76 77 func getAmount()->Double{ 78 return amount 79 } 80 } 81 82コード
前提・実現したいこと
画像のoutputの欄に為替変換した金額を出力させたいです。
回答1件
あなたの回答
tips
プレビュー