前提・実現したいこと
UserDefaultsを利用し3次元配列を受け取り作業を行うプログラムを書きたいのですが、エラーが起きてしまいシミュレーションまで移れません。
回答いただけると幸いです。
エラーが起きている中で行いたい事象。
data[p][q][0]、data[p][q][1]の中身を加算してUserDefaultsから保存し直したい。
発生している問題・エラーメッセージ
エラーメッセージ 39行目 Definition conflicts with previous value Type annotation missing in pattern Value of type 'Int' has no subscripts 41行目 Type annotation missing in pattern Value of type 'Int' has no subscripts
該当のソースコード
swift
1import UIKit 2 3 4 5class RecordDo: UIViewController,UIPickerViewDelegate,UIPickerViewDataSource { 6 7 @IBOutlet weak var chara1select: UIPickerView! 8 @IBOutlet weak var chara2select: UIPickerView! 9 @IBOutlet weak var resultselect: UIPickerView! 10 11 var twoDimArray = [[String]]() 12 13 var data: [[[Int]]] = [[[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]]] 14 15 let sampledata: [[[Int]]] = [[[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]]] 16 17 18 var ans = ["", "", ""] 19 var trueans = [0, 0, 0] 20 21 override func viewDidLoad() { 22 super.viewDidLoad() 23 24 let userDefaults = UserDefaults.standard 25 let data: [[[Int]]] = userDefaults.array(forKey: "SSB_DATA") as! [[[Int]]] 26 27 let p:Int = trueans[0] 28 let q:Int = trueans[1] 29 let r:Int = data[p][q][0] 30 31 let data;[p][q][0] = r 32 if trueans[2] == 0 { 33 let data;[p][q][1] = 1 34 } 35 36 chara1select.delegate = self 37 chara1select.dataSource = self 38 39 chara2select.delegate = self 40 chara2select.dataSource = self 41 42 resultselect.delegate = self 43 resultselect.dataSource = self 44 45 46 for _ in 0 ... 2{ 47 twoDimArray.append([]) 48 } 49 twoDimArray[0] = ["A","B","C","D","E","F","G","H","I","J","K","L"] 50 twoDimArray[1] = ["A","B","C","D","E","F","G","H","I","J","K","L"] 51 twoDimArray[2] = ["勝ち","負け"] 52 53 // Do any additional setup after loading the view. 54 chara1Label.text = "使用したキャラクター" 55 chara2Label.text = "対戦相手のキャラクター" 56 resultLabel.text = "勝敗" 57 58 } 59 60 override func didReceiveMemoryWarning() { 61 super.didReceiveMemoryWarning() 62 } 63 64 func numberOfComponents(in pickerView: UIPickerView) -> Int { 65 return 1 66 } 67 func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 68 return twoDimArray[pickerView.tag].count 69 } 70 71 func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 72 73 return twoDimArray[pickerView.tag][row] 74 75 } 76 77 func pickerView(_ pickerView: UIPickerView, 78 didSelectRow row: Int, 79 inComponent component: Int) { 80 if pickerView.tag == 0 { 81 ans[0] = twoDimArray[0][row] 82 } else if pickerView.tag == 1 { 83 ans[1] = twoDimArray[1][row] 84 } else if pickerView.tag == 2 { 85 ans[2] = twoDimArray[2][row] 86 } 87 88 for i in 0..<2 { 89 for j in 0..<12 { 90 if ans[i] == twoDimArray[0][j] { 91 trueans[i] = j 92 } 93 } 94 } 95 96 if ans[2] == "勝ち" { 97 trueans[2] = 0; 98 } else { 99 trueans[2] = 1; 100 } 101 102 test.text = "(trueans[0]) : (trueans[1]) : (trueans[2])" 103 } 104 105 @IBOutlet weak var chara1Label: UILabel! 106 @IBOutlet weak var chara2Label: UILabel! 107 @IBOutlet weak var resultLabel: UILabel! 108 109 110 111 @IBOutlet weak var test: UILabel! 112 113 @IBAction func moveRecordResult(_ sender: Any) { 114 115 116 userDefaults.set(data, forKey: "SSB_DATA") 117 userDefaults.synchronize() 118 } 119 120}
試したこと
解説サイトなどを巡ってみたのですが理解が追いつかずエラーを直すことができませんでした。
補足情報(FW/ツールのバージョンなど)
Xcode12
swift5.1使用。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。