swift4
xcode9
ボタンをタップしたら、突然Thread 1: signal SIGABRTが出ました。
全部のOutletとActionの紐付けを解除し、もう1度つなぎなおしました。
変数名も一部前と変更しました。
しかし、解決しません。
ResultViewController
1import UIKit 2 3class ResultViewController: UIViewController { 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 8 // Do any additional setup after loading the view. 9 displayresult() 10 result() 11 } 12 13 override func didReceiveMemoryWarning() { 14 super.didReceiveMemoryWarning() 15 // Dispose of any resources that can be recreated. 16 } 17 18 @IBOutlet weak var PLAYERLabel: UILabel! 19 20 @IBOutlet weak var CPUULabel: UILabel! 21 22 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 23 if segue.identifier == "WIN" { 24 let gameover_instance = segue.destination as! GameOverViewController 25 self.score = 1 26 gameover_instance.Score += self.score 27 } 28 29 } 30 31 32 //勝敗の判定を表す変数 33 var winorlose = "" 34 35 //スコアを保持する変数 36 var score = 0 37 38 //ChoiceHandViewControllerから渡される値を格納する変数 39 var Player_Hand = "" 40 var CPU_Hand = "" 41 42 //両者の手を表示 43 func displayresult() { 44 //プレイヤーの手を表示 45 PLAYERLabel.text = Player_Hand 46 //CPUの手を表示 47 CPUULabel.text = CPU_Hand 48 } 49 50 //勝敗を判定する処理 51 func result() { 52 if Player_Hand == CPU_Hand { 53 winorlose = "lose" 54 print("bbbb") 55 } else { 56 winorlose = "win" 57 print("aaaa") 58 } 59 } 60 61 62 //プレイヤーが勝ちなら前画面に戻る、負けならゲーム終了 63 @IBAction func ChangeViewButton(_ sender: Any) { 64 if winorlose == "win" { 65 performSegue(withIdentifier: "WIN", sender: nil) 66 } else if winorlose == "lose" { 67 performSegue(withIdentifier: "LOSE", sender: nil) 68 } 69 } 70 71 72 /* 73 // MARK: - Navigation 74 75 // In a storyboard-based application, you will often want to do a little preparation before navigation 76 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 77 // Get the new view controller using segue.destinationViewController. 78 // Pass the selected object to the new view controller. 79 } 80 */ 81 82}
GameOverViewController
1import UIKit 2 3class GameOverViewController: UIViewController { 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 8 // Do any additional setup after loading the view. 9 10 display_score() 11 } 12 13 override func didReceiveMemoryWarning() { 14 super.didReceiveMemoryWarning() 15 // Dispose of any resources that can be recreated. 16 } 17 18 @IBOutlet weak var ScoreLabel: UILabel! 19 20 21 //ResultViewControllerで渡されたscoreを格納する変数 22 var Score = 0 23 24 //勝った回数を表示 25 func display_score() { 26 ScoreLabel.text = "(Score)" 27 } 28 29 30 /* 31 // MARK: - Navigation 32 33 // In a storyboard-based application, you will often want to do a little preparation before navigation 34 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 35 // Get the new view controller using segue.destinationViewController. 36 // Pass the selected object to the new view controller. 37 } 38 */ 39 40}
下部がMainStoryBoard、コンソール、エラー箇所の画像です。
どこが原因なのか教えていただけないでしょうか?
よろしくお願いします。
「追記」
上記のエラーコードがこちらです。
Could not cast value of type 'MyAcchiMuiteHoi.ChoiceHandViewController' (0x108a8ba50) to 'MyAcchiMuiteHoi.GameOverViewController' (0x108a8b990). 2018-08-22 09:38:55.508647+0900 MyAcchiMuiteHoi[8071:235142] Could not cast value of type 'MyAcchiMuiteHoi.ChoiceHandViewController' (0x108a8ba50) to 'MyAcchiMuiteHoi.GameOverViewController' (0x108a8b990). (lldb)
let gameover_instance = segue.destination as! GameOverViewController の上に print(segue.destination) を追加して出力を教えて下さい。
<MyAcchiMuiteHoi.ChoiceHandViewController: 0x7fcf1dc29210> <MyAcchiMuiteHoi.ChoiceHandViewController: 0x7fcf1dc29210> Could not cast value of type 'MyAcchiMuiteHoi.ChoiceHandViewController' (0x10864da78) to 'MyAcchiMuiteHoi.GameOverViewController' (0x10864d9b8). 2018-08-22 09:36:59.398950+0900 MyAcchiMuiteHoi[7979:233301] Could not cast value of type 'MyAcchiMuiteHoi.ChoiceHandViewController' (0x10864da78) to 'MyAcchiMuiteHoi.GameOverViewController' (0x10864d9b8). (lldb)
print(segue.destination)がない場合が、こちらです。Could not cast value of type 'MyAcchiMuiteHoi.ChoiceHandViewController' (0x108a8ba50) to 'MyAcchiMuiteHoi.GameOverViewController' (0x108a8b990). 2018-08-22 09:38:55.508647+0900 MyAcchiMuiteHoi[8071:235142] Could not cast value of type 'MyAcchiMuiteHoi.ChoiceHandViewController' (0x108a8ba50) to 'MyAcchiMuiteHoi.GameOverViewController' (0x108a8b990). (lldb)
質問内に追記して下さい。(改行位置が分かりにくいです)
``` で囲ってもらえますか‥。
エラーを見ると、ChoiceHandViewControllerに遷移しようとしていますが?(print追加前のログで十分です。print(segue.destination)を追加したときのログは不要なので削除して下さい)
はい、もしwinorlose変数の値が"WIN"(Player_HandとCPU_Handが一致しない場合)なら、ChoiceHandViewControllerに遷移するようになってます。
今シミュレータで試したのですが、GameOverVIewControllerへ遷移する場合は正常に動作します。おそらく、ChoiceViewControllerへ遷移する際の処理に問題があるかと思います。
回答1件
あなたの回答
tips
プレビュー