質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

iPhone

iPhoneとは、アップル社が開発・販売しているスマートフォンです。 同社のデジタルオーディオプレーヤーiPodの機能、電話機能、インターネットやメールなどのWeb通信機能の3つをドッキングした機器です。

Q&A

解決済

1回答

2238閲覧

遊戯王電卓アプリ

tamago0224

総合スコア71

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

iPhone

iPhoneとは、アップル社が開発・販売しているスマートフォンです。 同社のデジタルオーディオプレーヤーiPodの機能、電話機能、インターネットやメールなどのWeb通信機能の3つをドッキングした機器です。

0グッド

1クリップ

投稿2015/03/24 05:10

swiftで練習がてら遊戯王の電卓を作ってみたのですが、
コメント//ここでエラー
の箇所で以下のようなエラー文が出ました

Thread 1: EXC_BAD_INSTRUCTION(code=EXC_1386_INVOP,subcode=0X0)

なぜこのようなことになるのでしょうか?
タイミングとしてはアプリをシュミレータで起動後、数字ボタンを押したときにおきます。

lang

1 2import UIKit 3 4class ViewController: UIViewController { 5 6 override func viewDidLoad() { 7 super.viewDidLoad() 8 // Do any additional setup after loading the view, typically from a nib. 9 } 10 11 override func didReceiveMemoryWarning() { 12 super.didReceiveMemoryWarning() 13 // Dispose of any resources that can be recreated. 14 } 15 16 @IBOutlet weak var Player1Display: UILabel! 17 18 @IBOutlet weak var Player2Display: UILabel! 19 20 @IBOutlet weak var operation: UILabel! 21 22 var playerSpecified: Int! 23 24 var result: Int! = nil 25 var currentNumStr = ["",""] 26 var currentOpe: String! = nil 27 28 29 @IBOutlet weak var segmentedControl: UISegmentedControl! 30 31 func setLabelResult(info: (val: Int, error: String?)) { 32 var text: String = "\(info.val)" 33 if (info.error != nil) { 34 calcClear() 35 text = info.error! 36 } 37 if playerSpecified == 0 { 38 Player1Display.text = text 39 } else { 40 Player2Display.text = text 41 } 42 } 43 44 func setLabelOpe() { 45 var text = "" 46 if currentOpe != nil { 47 text = "\(currentOpe)" 48 } 49 operation.text = text 50 } 51 52 func calcInit() { 53 calcClear() 54 setLabelResult((8000,nil)) 55 } 56 57 func calcClear() { 58 result = nil 59 if playerSpecified == 0 { 60 currentNumStr[0] = "" 61 } else { 62 currentNumStr[1] = "" 63 } 64 currentOpe = nil 65 setLabelOpe() 66 } 67 68 func calc(num1: Int,ope: String,num2: Int) -> (Int,String?) { 69 70 let calcFuncs:[String: (Int,Int) -> (Int,String?)] = [ 71 "=": calc_eq, 72 "+": calc_add, 73 "-": calc_sub 74 ] 75 76 return calcFuncs[ope]!(num1,num2) 77 } 78 79 func calc_eq(num1: Int,num2: Int) -> (Int,String?) { 80 return (num2,nil) 81 } 82 func calc_add(num1: Int,num2: Int) -> (Int,String?) { 83 return (num1 + num2,nil) 84 } 85 func calc_sub(num1: Int,num2: Int) -> (Int,String?) { 86 return (num1 - num2,nil) 87 } 88 89 @IBAction func btnNumber(sender: UIButton) { 90 let v = sender.titleLabel?.text! 91 if v == "0" { 92 if currentNumStr[playerSpecified] != "" { 93 currentNumStr[playerSpecified] = currentNumStr[playerSpecified] + v! 94 } else { 95 currentNumStr[playerSpecified] = "0" 96 } 97 } else { 98 //ここでエラー 99 currentNumStr[playerSpecified] = currentNumStr[playerSpecified] + v! 100 } 101 102 var x: Int = 0 103 if currentNumStr[playerSpecified] != "" { 104 x = currentNumStr[playerSpecified].toInt()! 105 } 106 setLabelResult((x,nil)) 107 108 } 109 110 @IBAction func btnOperate(sender: UIButton) { 111 var v = sender.titleLabel?.text 112 var rets: (val: Int,error: String?) = (0,nil) 113 114 if result != nil && currentNumStr[playerSpecified] != "" && currentOpe != nil { 115 rets = calc(result, ope: currentOpe, num2: currentNumStr[playerSpecified].toInt()!) 116 setLabelResult(rets) 117 if rets.error == nil { 118 result = rets.val 119 } 120 currentNumStr[playerSpecified] = "" 121 } else { 122 if currentNumStr[playerSpecified] != "" { 123 result = currentNumStr[playerSpecified].toInt()! 124 } 125 currentNumStr[playerSpecified] = "" 126 } 127 128 if v == "=" { 129 currentOpe = nil 130 } else { 131 currentOpe = v 132 } 133 setLabelOpe() 134 } 135 136 @IBAction func btnClear(sender: UIButton) { 137 calcInit() 138 } 139 140 //プレイヤー切り替え 141 @IBAction func changePlayer(sender: UISegmentedControl) { 142 switch segmentedControl.selectedSegmentIndex { 143 case 0 : playerSpecified = 0 144 println("playerSpecified is \(playerSpecified)") 145 case 1 : playerSpecified = 1 146 println("playerSpecified is \(playerSpecified)") 147 default : break 148 } 149 150 } 151 152 153 154 155 156 157}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

すべてコードを読んで理解したわけでもないので(IB接続多くてわからない)ぱっと見の解答ですが、playerSpecifiedの初期値が設定されていないからだと思います。
宣言時に

lang

1var playerSpecified: Int!

のところでちゃんと初期値を入れてあげればどうでしょうか

投稿2015/05/01 08:31

simorgh3196

総合スコア157

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問