前提・実現したいこと
swift4.0で配列の1つをランダムで表示させるシステムを作っています。
また配列の中身を別のコントローラーで削除して反映させるようにしたいです。
データを写すとString型からInt型への変更と、データの受け渡しで苦戦しております。
発生している問題・エラーメッセージ
エラーメッセージ Cannot assign value of type '[String]' to type 'String' 'subscript' is unavailable: cannot subscript String with an Int, see the documentation comment for discussion
該当のソースコード
ソースコード import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. display.text = "PUSH START TO PLAY" } @IBOutlet weak var display: UILabel! @IBOutlet weak var stanceLabel: UILabel! var stance = ["SWITCH", ""] @IBOutlet weak var approachLabel: UILabel! let approach = ["FRONTSIDE", "BACKSIDE"] @IBOutlet weak var spinLabel: UILabel! var recieveValue = "" @IBOutlet weak var grabLabel: UILabel! let grab = ["INDY", "MUTE", "STALE", "MELON"] @IBAction func startButton(_ sender: Any) { stanceLabel.text = stance[Int(arc4random()) % stance.count] approachLabel.text = approach[Int(arc4random()) % approach.count] spinLabel.text = recieveValue[Int(arc4random()) % recieveValue.count] print(recieveValue) grabLabel.text = grab[Int(arc4random()) % grab.count] } } ※下記より2つめのビュー import UIKit class SettingViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return(spin.count) } var spin = ["180", "360", "540", "720", "900", "1080", "1260", "1440"] @IBOutlet weak var spinTable: UITableView! override func viewDidLoad() { super.viewDidLoad() spinTable.delegate = self spinTable.dataSource = self // Do any additional setup after loading the view. } func tableView(_ tableView: UITableView, didUpdateFocusIn context: UITableViewFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) { print(spin.count) } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "spinCell") cell.textLabel?.text = spin[indexPath.row] return(cell) } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { } func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { //表示されたDeleteをタップしたか print(indexPath.row) spin.remove(at: indexPath.row) tableView.deleteRows(at: [indexPath], with: .fade) } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { //遷移先のViewControllerを取得 let ViewController = segue.destination as! ViewController ViewController.recieveValue = spin } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destination. // Pass the selected object to the new view controller. } */ }
試したこと
ググった変更方法は試しましたが、なかなかうまくいきません。
まだ独学で1週間ほどしか行なっていないので、理解も浅いです。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/12/27 03:16
2018/12/27 06:52
2018/12/27 09:41
2019/01/04 07:26