前提・実現したいこと
Swift初心者です。
現在、Swiftで複数選択可能なアンケートを作成しています。
該当するボタンを選択し、確認画面へ遷移すると、選択した値が表示される仕組みにしようと思っています。選択したボタンの値を空の配列にappendする所までは出来ましたが、appendした配列内の選択した値を削除するところでエラーが出てしまいます。
【例】
カレーライスとラーメン、ハンバーグを選択
↓
カレーライスを再び選択(このときエラーは出ず、正しく削除されている)
↓
ラーメンを再び選択(エラーが出てしまう)
どうしてこの様なエラーが出るのか分からないため、ご教授よろしくお願いします。
発生している問題・エラーメッセージ
Thread 1: Fatal error: Index out of range
該当のソースコード
Swift
1//AppDelegate.swift 2import UIKit 3 4@UIApplicationMain 5class AppDelegate: UIResponder, UIApplicationDelegate { 6 7 var window: UIWindow? 8 //空の配列を宣言 9 var Qhoge: [String]? = [] 10
Swift
1//ViewController.swift 2import UIKit 3 4class ViewController: UIViewController { 5 6 let QDataList = ["カレーライス", "ラーメン", "寿司", 7 "ハンバーグ", "焼き肉", "その他"] 8 var QappDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate 9 10 var tapCount = 0 11 var tapCount2 = 0 12 var tapCount3 = 0 13 var tapCount4 = 0 14 var tapCount5 = 0 15 var tapCount6 = 0 16 17 var remainder = 0 18 var remainder2 = 0 19 var remainder3 = 0 20 var remainder4 = 0 21 var remainder5 = 0 22 var remainder6 = 0 23 24 @IBAction func CurryBtn(_ sender: Any) { 25 tapCount = tapCount + 1 26 remainder = tapCount % 2 27 28 if remainder > 0 { 29 QappDelegate.Qhoge?.append(QDataList[0]) 30 print(QappDelegate.Qhoge!) 31 } else { 32 //ここでエラーが出る 33 QappDelegate.Qhoge?.remove(at: QDataList.index(of: "カレーライス")!) 34 print(QappDelegate.Qhoge!) 35 } 36 } 37 38 @IBAction func RamenBtn(_ sender: Any) { 39 tapCount2 = tapCount2 + 1 40 remainder2 = tapCount2 % 2 41 42 if remainder2 > 0 { 43 QappDelegate.Qhoge?.append(QDataList[1]) 44 print(QappDelegate.Qhoge!) 45 } else { 46 //ここでエラーが出る 47 QappDelegate.Qhoge?.remove(at: QDataList.index(of: "ラーメン")!) 48 print(QappDelegate.Qhoge!) 49 } 50 } 51 52 @IBAction func SushiBtn(_ sender: Any) { 53 tapCount3 = tapCount3 + 1 54 remainder3 = tapCount3 % 2 55 56 if remainder3 > 0 { 57 QappDelegate.Qhoge?.append(QDataList[2]) 58 print(QappDelegate.Qhoge!) 59 } else { 60 //ここでエラーが出る 61 QappDelegate.Qhoge?.remove(at: QDataList.index(of: "寿司")!) 62 print(QappDelegate.Qhoge!) 63 } 64 } 65 66 @IBAction func HamburgerBtn(_ sender: Any) { 67 tapCount4 = tapCount4 + 1 68 remainder4 = tapCount4 % 2 69 70 if remainder4 > 0 { 71 QappDelegate.Qhoge?.append(QDataList[3]) 72 print(QappDelegate.Qhoge!) 73 } else { 74 //ここでエラーが出る 75 QappDelegate.Qhoge?.remove(at: QDataList.index(of: "ハンバーグ")!) 76 print(QappDelegate.Qhoge!) 77 } 78 } 79 80 @IBAction func GrilledMeatBtn(_ sender: Any) { 81 tapCount5 = tapCount5 + 1 82 remainder5 = tapCount5 % 2 83 84 if remainder5 > 0 { 85 QappDelegate.Qhoge?.append(QDataList[4]) 86 print(QappDelegate.Qhoge!) 87 } else { 88 //ここでエラーが出る 89 QappDelegate.Qhoge?.remove(at: QDataList.index(of: "焼き肉")!) 90 print(QappDelegate.Qhoge!) 91 } 92 } 93 94 @IBAction func OtherwiseBtn(_ sender: Any) { 95 tapCount6 = tapCount6 + 1 96 remainder6 = tapCount6 % 2 97 98 if remainder6 > 0 { 99 QappDelegate.Qhoge?.append(QDataList[5]) 100 print(QappDelegate.Qhoge!) 101 } else { 102 //ここでエラーが出る 103 QappDelegate.Qhoge?.remove(at: QDataList.index(of: "その他")!) 104 print(QappDelegate.Qhoge!) 105 } 106 } 107 108 @IBAction func NextBtn(_ sender: Any) { 109 let storyboard: UIStoryboard = self.storyboard! 110 let Confimation = storyboard.instantiateViewController(withIdentifier: "q") 111 self.present(Confimation, animated:true, completion: nil) 112 } 113 override func viewDidLoad() { 114 super.viewDidLoad() 115 // Do any additional setup after loading the view, typically from a nib. 116 } 117 118 override func didReceiveMemoryWarning() { 119 super.didReceiveMemoryWarning() 120 // Dispose of any resources that can be recreated. 121 } 122 123 124}
Swift
1//Confirmation.swift 2import UIKit 3 4class Confirmation: UIViewController, UIScrollViewDelegate { 5 6 var QappDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate 7 8 var QrecievedHoge: [String] = [] 9 10 var CurryLabel = UILabel() 11 var RamenLabel = UILabel() 12 var SushiLabel = UILabel() 13 var HamburgerLabel = UILabel() 14 var GrilledMeatLabel = UILabel() 15 var OtherwiseLabel = UILabel() 16 17 override func viewDidLoad() { 18 super.viewDidLoad() 19 20 QrecievedHoge = QappDelegate.Qhoge! 21 22 let scrollView = UIScrollView() 23 scrollView.backgroundColor = UIColor.blue 24 //表示窓のサイズと位置を指定 25 scrollView.frame.size = CGSize(width: 400, height: 520) 26 scrollView.frame = CGRect(x: 350, y: 221, width: 400, height: 200) 27 //中身の大きさを設定 28 scrollView.contentSize = CGSize(width: 400, height: 480) 29 //スクロールの跳ね返り 30 scrollView.bounces = false 31 //スクロールバーの見た目と余白 32 scrollView.indicatorStyle = .white 33 scrollView.delegate = self 34 35 for i in 0 ..< QrecievedHoge.count { 36 switch QrecievedHoge[i] { 37 case "カレーライス": 38 CurryLabel.text = QrecievedHoge[i] 39 CurryLabel.sizeToFit() 40 CurryLabel.backgroundColor = UIColor.white 41 CurryLabel.center = CGPoint(x: 175, y: 30*(i+1)) 42 scrollView.addSubview(CurryLabel) 43 case "ラーメン": 44 RamenLabel.text = QrecievedHoge[i] 45 RamenLabel.sizeToFit() 46 RamenLabel.backgroundColor = UIColor.white 47 RamenLabel.center = CGPoint(x: 175, y: 30*(i+1)) 48 scrollView.addSubview(RamenLabel) 49 case "寿司": 50 SushiLabel.text = QrecievedHoge[i] 51 SushiLabel.sizeToFit() 52 SushiLabel.backgroundColor = UIColor.white 53 SushiLabel.center = CGPoint(x: 175, y: 30*(i+1)) 54 scrollView.addSubview(SushiLabel) 55 case "ハンバーグ": 56 HamburgerLabel.text = QrecievedHoge[i] 57 HamburgerLabel.sizeToFit() 58 HamburgerLabel.backgroundColor = UIColor.white 59 HamburgerLabel.center = CGPoint(x: 175, y: 30*(i+1)) 60 scrollView.addSubview(HamburgerLabel) 61 case "焼き肉": 62 GrilledMeatLabel.text = QrecievedHoge[i] 63 GrilledMeatLabel.sizeToFit() 64 GrilledMeatLabel.backgroundColor = UIColor.white 65 GrilledMeatLabel.center = CGPoint(x: 175, y: 30*(i+1)) 66 scrollView.addSubview(GrilledMeatLabel) 67 case "その他": 68 OtherwiseLabel.text = QrecievedHoge[i] 69 OtherwiseLabel.sizeToFit() 70 OtherwiseLabel.backgroundColor = UIColor.white 71 OtherwiseLabel.center = CGPoint(x: 175, y: 30*(i+1)) 72 scrollView.addSubview(OtherwiseLabel) 73 default: break 74 } 75 } 76 self.view.addSubview(scrollView) 77 } 78 79 override func didReceiveMemoryWarning() { 80 super.didReceiveMemoryWarning() 81 // Dispose of any resources that can be recreated. 82 } 83} 84
補足情報(FW/ツールのバージョンなど)
使用ツール:Xcode9.4 使用言語:Swift4
Simulator:iPad Pro(9.7-inch)の横画面に限定
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/06/11 13:19