iosswiftnscoding

How can I save user cart on app close? - swift


I want to save current user cart when the app closes, so then the user will be able to return from the same state.

I've this class:

class CarrinhoItem: NSObject, NSCoding {
    let produto: Item
    var qtd: Double

    // MARK: - Init

    init(_ produto: Item, _ qtd: Double) {
        self.produto = produto
        self.qtd = qtd
    }

    // MARK: - NSCoding
    func encode(with aCoder: NSCoder) {
        aCoder.encode(produto, forKey: "produto")
        aCoder.encode(qtd, forKey: "qtd")
    }

    required init?(coder aDecoder: NSCoder) {
        produto = aDecoder.decodeObject(forKey: "produto") as! Item
        qtd = aDecoder.decodeDouble(forKey: "qtd")

    }
}

And I'm using these functions to return and save the cart:

class CarrinhoDao {

    func save(_ Carrinho: [CarrinhoItem]) {
        guard let caminho = recuperaCaminho() else { return }
        do {
            let dados = try NSKeyedArchiver.archivedData(withRootObject: Carrinho, requiringSecureCoding: false)
            try dados.write(to: caminho)
        } catch {
            print(error.localizedDescription)
        }
    }

    func recupera() -> [CarrinhoItem] {
        guard let caminho = recuperaCaminho() else { return [] }
        do {
            let dados = try Data(contentsOf: caminho)
            guard let CarrinhoSalvo = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(dados) as? Array<CarrinhoItem> else { return [] }
            return CarrinhoSalvo
        } catch {
            print(error.localizedDescription)
            return []
        }
    }

    func recuperaCaminho() -> URL? {
        guard let diretorio = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return nil }
        let caminho = diretorio.appendingPathComponent("refeicao")

        return caminho
    }
}

I've put the func save on the AppDelegate:

CartService.currentCart = [CarrinhoItem1,CarrinhoItem2, ...]

fun applicationWillTerminate(_ application: UIApplication) {
        CarrinhoDao().save(CartService.currentCart)
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }

but I'm getting this error:

unrecognized selector sent to instance 0x6000027f5780
The data couldn’t be written because it isn’t in the correct format.

edit:

Solved adding the nscoding for the item class:

func encode(with aCoder: NSCoder) {
        aCoder.encode(nome, forKey: "nome")
        aCoder.encode(imagem, forKey: "imagem")
        aCoder.encode(preco, forKey: "preco")
        aCoder.encode(precoantes, forKey: "precoantes")
        aCoder.encode(tipo, forKey: "tipo")
        aCoder.encode(aval, forKey: "aval")
        aCoder.encode(avalcount, forKey: "avalcount")
        aCoder.encode(disponivel, forKey: "disponivel")
        aCoder.encode(descricao, forKey: "descricao")


    }

    required init?(coder aDecoder: NSCoder) {
        nome = aDecoder.decodeObject(forKey: "nome") as! String
        imagem = aDecoder.decodeObject(forKey: "imagem") as! String
        preco = aDecoder.decodeDouble(forKey: "preco")
        precoantes = aDecoder.decodeObject(forKey: "precoantes") as! String
        tipo = aDecoder.decodeObject(forKey: "tipo") as! String
        aval = aDecoder.decodeInteger(forKey: "aval")
        avalcount = aDecoder.decodeInteger(forKey: "avalcount")
        disponivel = aDecoder.decodeInteger(forKey: "disponivel")
        descricao = aDecoder.decodeObject(forKey: "descricao") as! String
    }

Solution

  • Item is a custom class, so you must use NSCoding protocol for this class as well