swift
1extension String { 2 func numberOfOccurrences(of word: String) -> Int { 3 var count = 0 4 var nextRange = self.startIndex..<self.endIndex 5 while let range = self.range(of: word, options: .caseInsensitive, range: nextRange) { 6 count += 1 7 nextRange = range.upperBound..<self.endIndex 8 } 9 return count 10 } 11} 12 13import UIKit 14 15class ViewController: UIViewController { 16 17 @IBOutlet weak var OverrallLayout: UIView! 18 @IBOutlet weak var CalcProc: UIView! 19 @IBOutlet weak var Proctxt: UITextField! 20 @IBOutlet weak var Rsltlbl: UILabel! 21 @IBOutlet weak var CalcRslt: UIView! 22 var dataSource = DataSource() 23 24 @IBAction func Back(_ sender: Any) { 25 } 26 27 @IBAction func Next(_ sender: Any) { 28 } 29 30 @IBAction func Histoty(_ sender: Any) { 31 } 32 33 @IBAction func GT(_ sender: Any) { 34 } 35 36 @IBAction func Root(_ sender: Any) { 37 } 38 39 @IBAction func Percent(_ sender: Any) { 40 } 41 42 @IBAction func Division(_ sender: Any) { 43 } 44 45 @IBAction func Save(_ sender: Any) { 46 } 47 48 @IBAction func Mul(_ sender: Any) { 49 } 50 51 @IBAction func Subtract(_ sender: Any) { 52 } 53 54 @IBAction func Add(_ sender: Any) { 55 } 56 57 @IBAction func Delete(_ sender: Any) { 58 /*var str = Rsltlbl.text 59 var startIndex = (String((Rsltlbl.text?.suffix((Rsltlbl.text?.count)! - 1))!)) 60 str?.remove(at:startIndex)*/ 61 62 /*var str = Rsltlbl.text! 63 let startIndex = (String((Rsltlbl.text?.suffix((Rsltlbl.text?.count)! - 1))!)) 64 let endIndex = str.index(str.endIndex, offsetBy: -1) 65 str.removeSubrange() // "こんift"*/ 66 } 67 68 @IBAction func SignChange(_ sender: Any) { 69 } 70 71 @IBAction func Clear(_ sender: Any) { 72 dataSource.clear() 73 labelUpdate() 74 } 75 76 @IBAction func tapNumber(_ sender: UIButton) { 77 let text = sender.tag == 10 ? "." : String(sender.tag) 78 dataSource.upDate(value: text) 79 labelUpdate() 80 81 } 82 func labelUpdate() { 83 Proctxt.text = dataSource.num 84 Rsltlbl.text = dataSource.num 85 86 print(dataSource.num) 87 print(dataSource.numtext) 88 } 89 90 class DataSource { 91 92 enum InputType: Int { 93 case p,f 94 } 95 96 var inputType: InputType = .p 97 var num = "" 98 var numtext = "" 99 100 101 func upDate(value: String) { 102 switch inputType { 103 case .p: 104 guard (num + value).numberOfOccurrences(of: ".") < 2 else { return } 105 num += value 106 case .f: 107 guard (numtext + value).numberOfOccurrences(of: ".") < 2 else { return } 108 numtext += value 109 } 110 } 111 112 func clear() { 113 switch inputType { 114 case .p: 115 num = "" 116 case .f: 117 numtext = "" 118 } 119 } 120 } 121 122 123 override func viewDidLoad() { 124 super.viewDidLoad() 125 // Do any additional setup after loading the view, typically from a nib. 126 } 127 128 override func didReceiveMemoryWarning() { 129 super.didReceiveMemoryWarning() 130 // Dispose of any resources that can be recreated. 131 } 132 133 134} 135
困っていること
練習で電卓を作っています。
tapNumber
に9~0、.のボタンを結びつけています。
numberOfOccurrences
関数で例えば1と入力したら1と表示欄に出力するようにしているのですがどのボタンをタップしても0としか表示されなくて困っています。
デバッグエリアでも確認してみましたが0しか表示されませんでした。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/09/11 03:04