let userDefaults = UserDefaults.standard
方法1
// userDefaultsに格納したい値
let count: [[Int]] = [[1, 2, 3, 4, 5],[1, 2, 3, 4, 5]]
// 配列の保存
userDefaults.set(count, forKey: “udCount”)
// userDefaultsに保存された値の取得
var getCount: [[Int]] = userDefaults.array(forKey: “udCount”) as! [[Int]]
// 追加したい値
let addData: [Int] =[1, 2, 3, 4, 5]
// 値の追加
getCount.append(addData)
方法2
// ArchiveしてUserDefaultに格納
let rootObject: [[Int]] = [[1, 2, 3, 4, 5],[1, 2, 3, 4, 5]]
let archivedData = try! NSKeyedArchiver.archivedData(withRootObject: rootObject, requiringSecureCoding: false)
UserDefaults.standard.set(archivedData, forKey: “key”)
// UserDefaultからゲットしてUnarchiveし、値を取得
let data = UserDefaults.standard.data(forKey: “key”)!
let unarchivedObject = try! NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? [[Int]]
print(unarchivedObject as Any) // => Optional([[1, 2, 3, 4, 5], [1, 2, 3, 4, 5]])