I'm an Android developer and I'm trying to learn some things for iOS as well.
I have knocked myself a little on simple things, like saving persisting an object of mine to catch it again another time. On Android I would use a SharedPreference
and would solve everything.
In Swift 3 I'm not sure how to do this. I have already seen that NSUserDefaults
does not save objects.
Later I tried to use NSCoding
, which looks like this:
class Appointment: NSObject, NSCoding {
let custoMercadoria: Double
let custoServico: Double
let aliquotaSimples: Double
let margem: Double
let fretePreco: Double
let cobrancaPreco: Double
let otherCost: Double
override init(){
}
init(Appointment : (Double,Double,Double,Double,Double,Double,Double)){
self.custoMercadoria = Appointment.0
self.custoServico = Appointment.1
self.aliquotaSimples = Appointment.2
self.margem = Appointment.3
self.fretePreco = Appointment.4
self.cobrancaPreco = Appointment.5
self.otherCost = Appointment.6
}
func getAppointment() -> (Double,Double,Double,Double,Double,Double,Double) {
return (self.custoMercadoria,self.custoServico,self.aliquotaSimples,self.margem,self.fretePreco,self.cobrancaPreco,self.otherCost)
}
required init(coder aDecoder: NSCoder) {
self.custoMercadoria = aDecoder.decodeObject(forKey:"custoMercadoria") as! Double
self.custoServico = aDecoder.decodeObject(forKey: "custoServico") as! Double
self.aliquotaSimples = aDecoder.decodeObject(forKey: "aliquotaSimples") as! Double
self.margem = aDecoder.decodeObject(forKey: "margem") as! Double
self.fretePreco = aDecoder.decodeObject(forKey: "fretePreco") as! Double
self.cobrancaPreco = aDecoder.decodeObject(forKey: "cobrancaPreco") as! Double
self.otherCost = aDecoder.decodeObject(forKey: "otherCost") as! Double
}
func encode(with aCoder: NSCoder){
aCoder.encode(self.custoMercadoria, forKey: "custoMercadoria")
aCoder.encode(self.custoServico, forKey: "custoServico")
aCoder.encode(self.aliquotaSimples, forKey: "aliquotaSimples")
aCoder.encode(self.margem, forKey: "margem")
aCoder.encode(self.fretePreco, forKey: "fretePreco")
aCoder.encode(self.cobrancaPreco, forKey: "cobrancaPreco")
aCoder.encode(self.otherCost, forKey: "otherCost")
}
}
I am in doubt if the class assembly is correct, initializing the attributes, encode and decode. If anyone has any examples of how to use it, thank you too.