質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

1081閲覧

Bool値をアプリ内で保存して‥アプリのバックグラウンドを一度消して再表示する方法

minoossp

総合スコア42

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2016/11/18 23:04

テーブルでスイッチを複数実装(各セルに一つずつ)した時にアプリ内で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]

これで保存した値を出せると思うのですが
エラーになってしまいます。

よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

回答依頼かけてから申し訳有りませんが解決しました。

Bool値のnilの時以外実行させるif文ができていなかっただけでした。

_Kentarouさん‥ご迷惑をお掛けして申し訳ありませんでした。

投稿2016/11/19 01:36

minoossp

総合スコア42

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問