1
I’m an Android developer and I’m trying to learn some things for iOS as well.
I have hit myself a little on simple things, like saving persist an object of mine to catch it again another time. On Android I would use a SharedPreference
and solve everything.
On Swift 3 I’m not sure how to do this. I’ve seen that the NSUserDefaults
unsaved objects.
Later I tried to use the NSCoding
, that has remained so:
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, by initializing the attributes, Match and Code. If anyone has any example of how to use also thank you.
Can’t I do it in a simpler way? I think it’s not very suitable to use a comic book, in this case
– LMaker
@Luangabriel depends on what you save, Nsuserdefaults was not made for you to save whole objects...
– Victor Cechinel