21
28

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.

userDefaultsを使った配列の保存・取得・操作

Posted at

userDefaultsを使用した1次元配列・2次元配列の扱い方について備忘録として掲載します。
仕様上、大量のデータの保存には向いていません。その際はDBを使った方が高速に動作するでしょう。

1次元配列の保存・取得

ViewController.swift
import UIKit

class ViewController: UIViewController {
    
    //  userDefaultsの定義
    var userDefaults = UserDefaults.standard

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        // userDefaultsに格納したい値
        let maxSpeed: [Double] = [120.05, 134.25, 80.5]
        // 配列の保存
        userDefaults.set(maxSpeed, forKey: "udMaxSpeed")
        
        // userDefaultsに保存された値の取得
        let getMaxSpeed:[Double] = userDefaults.array(forKey: "udMaxSpeed") as! [Double]
        print(getMaxSpeed)
    }

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

}
console
[120.05, 134.25, 80.5]

値の追加

ここから先はViewDidLoad()内の処理のみ記載します。

ViewController.swift
// userDefaultsに格納したい値
let maxSpeed: [Double] = [120.05, 134.25, 80.5]
// 配列の保存
userDefaults.set(maxSpeed, forKey: "udMaxSpeed")

// userDefaultsに保存された値の取得
var getMaxSpeed:[Double] = userDefaults.array(forKey: "udMaxSpeed") as! [Double]
// 値の追加
let addMaxSpeed: Double = 100.0
getMaxSpeed.append(addMaxSpeed)
print(getMaxSpeed)
console
[120.05, 134.25, 80.5, 100.0]

2次元配列の保存・取得

ViewController.swift
// userDefaultsに格納したい値
let count: [[Int]] = [[20, 34, 50, 12], [21, 44, 38, 40]]
// 配列の保存
userDefaults.set(count, forKey: "udCount")

// userDefaultsに保存された値の取得
let getCount: [[Int]] = userDefaults.array(forKey: "udCount") as! [[Int]]
print(getCount)
console
[[20, 34, 50, 12], [21, 44, 38, 40]]

1次元配列のデータを末尾に追加する

ViewController.swift
// userDefaultsに格納したい値
let count: [[Int]] = [[20, 34, 50, 12], [21, 44, 38, 40]]
// 配列の保存
userDefaults.set(count, forKey: "udCount")

// userDefaultsに保存された値の取得
var getCount: [[Int]] = userDefaults.array(forKey: "udCount") as! [[Int]]
// 追加したい値
let addData: [Int] = [10, 37, 62, 19, 43,85]
// 値の追加
getCount.append(addData)
print(getCount)
console
[[20, 34, 50, 12], [21, 44, 38, 40], [10, 37, 62, 19, 43, 85]]

追加したデータの削除

ViewController.swift
// userDefaultsに格納したい値
let count: [[Int]] = [[20, 34, 50, 12], [21, 44, 38, 40]]
// 配列の保存
userDefaults.set(count, forKey: "udCount")

// userDefaultsに保存された値の取得
var getCount: [[Int]] = userDefaults.array(forKey: "udCount") as! [[Int]]
// 追加したい値
let addData: [Int] = [10, 37, 62, 19, 43,85]
// 値の追加
getCount.append(addData)

// 末尾の1次元配列を削除
getCount.removeLast()
print(getCount)
console
[[20, 34, 50, 12], [21, 44, 38, 40]]
21
28
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
21
28

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?