1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Swift UserDefaultsについて

Last updated at Posted at 2018-03-01

自分用メモ

UserDefaultsはデバイス上でplistにデータを保存できるSwiftの built-in function. Foundation(モジュールって呼んで良いのかな?)のfunction.

RailsのActiveRecordのようなものと解釈。(ActiveRecordはデータベースのクエリラッパーなのでちょっと違うけど)

使い方(Todo list)

UserDefaultsインスタンスを作ってあげる。

var itemArray = []

let defaults = UserDefaults.standard

Todoリストで追加されるイベントが発火

let action = UIAlertAction(title: "Add Item", style: .default) { (action) in
            //what will happen once the user clicks the Add Item button on our UIAlert
            self.itemArray.append(textField.text!)
            
            //ここでdefaultsインスタンスにぶち込む。
            self.defaults.set(self.itemArray, forKey: "TodoListArray")
            
            self.tableView.reloadData()
            
}

これでデバイス内のplistファイルにTodoリストが保存される。

1
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?