テーブルでスイッチを複数実装(各セルに一つずつ)した時にアプリ内でSwitchのBool値を保存した時
配列に入ったBool値をアプリ内に保存(userdefault)して
アプリ内で保存されているか確認しようと思ったのすがうまくいきません。
userdefaultは
UserDefaults.standard.set(Switcharrye, forKey: "SwitcharryeKey")
Switcharryeは配列です
保存される直前ログはこの状態です
suittiarrye [true, true, true, true, true, true]
ここでUISwitchを動かして
falseに変更して
suittiarrye [false, true, true, true, true, true]
ここでuserdefaultに保存してから
一度アプリのバックグランドからアプリを消して
再度アプリを立ち上げた時に
立ち上げたログが
suittiarrye [true, true, true, true, true, true]
このようになってしまいます。
import UIKit class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { @IBOutlet weak var table: UITableView! var mySwicth:UISwitch = UISwitch() var Switcharrye: [Bool] = [true,true,true,true,true,true] var array = [String]() override func viewDidLoad() { super.viewDidLoad() table.delegate = self table.dataSource = self // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func numberOfSections(in tableView: UITableView) -> Int { // #warning Incomplete implementation, return the number of sections return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // #warning Incomplete implementation, return the number of rows return Switcharrye.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) ////////////////////////////////////////////////////// ////////////////////////////////////////////////////// let mySwicth: UISwitch = UISwitch() mySwicth.layer.position = CGPoint(x: cell.contentView.frame.width-60, y: cell.contentView.frame.height/2) // SwitchのOn/Off切り替わりの際に、呼ばれるイベントを設定する. mySwicth.addTarget(self, action: #selector(ViewController.onClickMySwicth(sender:)), for: UIControlEvents.valueChanged) cell.contentView.addSubview(mySwicth) ////////////////////////////////////////////////////// ////////////////////////////////////////////////////// UserDefaults.standard.set(Switcharrye, forKey: "Switcharrye") Switcharrye = UserDefaults.standard.object(forKey: "Switcharrye") as! [Bool] mySwicth.isOn = Switcharrye[indexPath.row] return cell } internal func onClickMySwicth(sender: UISwitch){ ////////////////////////////////////////////////////// ////////////////////////////////////////////////////// let cellPoint1 = table.convert(sender.center, from: sender.superview) let indexPath1 = table.indexPathForRow(at: cellPoint1) if sender.isOn { Switcharrye.remove(at: (indexPath1?.row)!) Switcharrye.insert(true, at: (indexPath1?.row)!) }else { Switcharrye.remove(at: (indexPath1?.row)!) Switcharrye.insert(false, at: (indexPath1?.row)!) } UserDefaults.standard.set(Switcharrye, forKey: "Switcharrye") mySwicth.isOn = Switcharrye[(indexPath1?.row)!] print("スイッチsuittiarrye \(Switcharrye)") } ////////////////////////////////////////////////////// ////////////////////////////////////////////////////// ////////////////////////////////////////////////////// ////////////////////////////////////////////////////// override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) // アプリ内で保存された配列を取り出す // ここに何を入れればいいですか? table.reloadData() } ////////////////////////////////////////////////////// ////////////////////////////////////////////////////// }
viewWillAppear内にどう基準すればいいかわかりません
Switcharrye = UserDefaults.standard.object(forKey: "Switcharrye") as! [Bool]
これで保存した値を出せると思うのですが
エラーになってしまいます。
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。