27
26

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でNSUserDefaultsを使ったデータの永続化のメモ

Last updated at Posted at 2014-06-26

アプリを停止させてもずっと保持しておきたい場合は、NSUserDefaultsを使うとそうしたデータを簡単に保持できます。
使い方としてはNSUserDefaultsを取得して、取得したNSUserDefaultsに対してデータを保存したり取り出したりすることでデータ保持を行います。
少ないレコード数を扱う場合に便利です。
web開発でいうクッキーくらいの扱いだといいのかな。

大量のデータを扱う場合にはこちらのCoreDataを使うのがおすすめ。
http://qiita.com/yukihamada/items/61ab1299d99fabdb9469

サンプル

var uid = NSUserDefaults.standardUserDefaults().stringForKey("uid")
    
if(uid){
    println("uid:"+uid)
}else{
    // uidを端末に記録
    NSUserDefaults.standardUserDefaults().setObject("値",forKey:"uid")
}
27
26
0

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
27
26

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?