Swift3で簡単な電卓アプリを作ろうとしていますが
ストーリーボードにボタンを配置した上で
UIKitの演算の入力箇所で
Binary operator '-' cannot be apllied two 'String' operands
とエラーが出るので
パラメータ(prev_label.textとlabel.text!)をInt型に一度変換して
計算する処理を書こうとしていますが
うまくいきません。
わかる方がいらっしゃればご指摘よろしくお願いします。
Swift
1import UIKit 2 3class ViewController: UIViewController { 4 @IBOutlet weak var label: UILabel! 5 @IBOutlet weak var prev_label: UILabel! 6 @IBOutlet weak var calc_label: UILabel! 7 8 override func viewDidLoad() { 9 super.viewDidLoad() 10 // Do any additional setup after loading the view, typically from a nib. 11 label.text = "" 12 prev_label.text = "" 13 calc_label.text = "" 14 } 15 override func didReceiveMemoryWarning() { 16 super.didReceiveMemoryWarning() 17 // Dispose of any resources that can be recreated. 18 } 19 @IBAction func num_button_pushed(_ sender: UIButton) { 20 label.text! += sender.titleLabel!.text! 21 } 22 @IBAction func calc_sign_pushed(_ sender: UIButton) { 23 calc_label.text = sender.titleLabel!.text! 24 prev_label.text = label.text 25 label.text = "" 26 } 27 @IBAction func equal_pushed(_ sender: Any) { 28 if prev_label.text != nil && label.text != nil{ 29 if calc_label.text == "+"{ 30//問題の箇所 31 label.text = String(prev_label.text! + label.text!)}else 32//Binary operator '-' cannot be apllied two 'String' operands 33if calc_label.text == "-"{ 34 label.text = String(prev_label.text! - label.text!)}else 35//Binary operator '*' cannot be apllied two 'String' operands 36if calc_label.text == "*"{ 37 label.text = String(prev_label.text! * label.text!)}else 38//Binary operator '/' cannot be apllied two 'String' operands 39if calc_label.text == "/"{ 40 label.text = String(prev_label.text! / label.text!)} 41 } 42 } 43 } 44 45
###自分で調べたこと
toInt()はSwift3では使えない
Int(パラメータ)を使おうとしたが書き方がわからない
###前提条件
Xcode8.3.2
Swift3
prev_labelは数字ボタンを2回続けて押した時に
初めの数字が入るラベル
labelは最新の数字と「=」ボタンを押した時に
表示が入るラベル
calc.labelは演算子ボタンを押した時に演算子が入るラベル
"-","","/"をコードした箇所にそれぞれ
Binary operator '-' cannot be apllied two 'String' operands
Binary operator '' cannot be apllied two 'String' operands
Binary operator '/' cannot be apllied two 'String' operands
というエラーが出る
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。