LoginSignup
1
2

More than 5 years have passed since last update.

UserDefaultsを使って永続化してみる Swift3.0

Posted at

コピペ要員として、書き残しておこうと思います


import UIKit

var jayZsongNameResult : String?

class ViewController: UIViewController {

    //構造体に永続化した値を入れておく
    struct artist {
        enum jayZ : String {
            case jayZsongName
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        //最初はnil
        print(UserDefaults.standard.string(forKey: artist.jayZ.jayZsongName.rawValue) as Any)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func save(_ sender: Any) {

        //永続化実行
        let jayZsongName = "4:44"
        UserDefaults.standard.set(jayZsongName, forKey: artist.jayZ.jayZsongName.rawValue)
        jayZsongNameResult = UserDefaults.standard.string(forKey: artist.jayZ.jayZsongName.rawValue)!
    }
}

ソース

GitHub

UserDefaultsを使って永続化してみる Swift3.0

1
2
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
1
2