How to store array list of custom objects to NSUserDefaults?

Since Swift 4 it is so much more easy using Codable:

import Foundation

//Declaration
struct Item: Codable {
    var name: String
    var collectionId: Int
}

//Initialization
let itemA: Item = Item.init(name: "Bob A.", collectionId: 1)
let itemB: Item = Item.init(name: "Bob B.", collectionId: 1)
let items: [Item] = [itemA, itemB]

//Storing Items
if let encoded = try? JSONEncoder().encode(items) {
    UserDefaults.standard.set(encoded, forKey: "items")
}

//Retrieving Items
do {
    let storedObjItem = UserDefaults.standard.object(forKey: "items")
    let storedItems = try JSONDecoder().decode([Item].self, from: storedObjItem as! Data)
    print("Retrieved items: \(storedItems)")
} catch let err {
    print(err)
}

First, you have to inherit Item from NSObject, with coder & decoder methods:

class Item :NSObject
{
    var name: String = ""
    var collectionId: Int = 0

    init(name: String, collectionId: Int) {
        self.name = name
        self.collectionId = collectionId
    }

    init(coder decoder: NSCoder) {
        self.name = decoder.decodeObjectForKey("name") as String
        self.collectionId = decoder.decodeIntegerForKey("collectionId")
    }

    func encodeWithCoder(coder: NSCoder) {
        coder.encodeObject(self.name, forKey: "name")
        coder.encodeInt(Int32(self.collectionId), forKey: "collectionId")
    }
}

Then, these are functions to insert & retrieve from NSUserDefaults.

func insertItems()
    {
        let defaults = NSUserDefaults.standardUserDefaults()

        let data = NSKeyedArchiver.archivedDataWithRootObject(itemList)
        NSUserDefaults.standardUserDefaults().setObject(data, forKey: "myList")
    }

func retrieveItems()
{
    let defaults = NSUserDefaults.standardUserDefaults()

    if let data = NSUserDefaults.standardUserDefaults().objectForKey("myList") as? NSData {
        let _mySavedList = NSKeyedUnarchiver.unarchiveObjectWithData(data) as [Item]
    }
}

Example:

let item1 = Item(name: "Item 1", collectionId: 1)
let item2 = Item(name: "Item 2", collectionId: 2)

itemList.append(item1)
itemList.append(item2)


insertItems()
retrieveItems()

Tags:

Ios

Swift