Almacenamiento persistente simple en Swift
Tengo una serie de objetos, cada uno con un número de propiedades. Aquí hay algunos datos de muestra tomados al recorrer la matriz de objetos:
Name = Rent
Default Value 750
This Months Estimate = 750
Sum Of This Months Actuals = 0
Risk Factor = 0.0
Monthly Average = 750.0
--------------
Name = Bills
Default Value 250
This Months Estimate = 170
Sum Of This Months Actuals = 140
Risk Factor = 0.0
Monthly Average = 190.0
--------------
Name = Food
Default Value 240
This Months Estimate = 200
Sum Of This Months Actuals = 95
Risk Factor = 0.0
Monthly Average = 190.0
--------------
Name = Lunches
Default Value 100
This Months Estimate = 150
Sum Of This Months Actuals = 155
Risk Factor = 0.899999976158142
Monthly Average = 190.0
Son muy pocos datos, así que quiero evitar el uso de datos básicos. Necesito poder persistir, guardar la matriz, luego abrirla nuevamente y poder recorrerla. Tenía la esperanza de usar una solución simple como NSUserDefaults o NSKeyedArchiver, pero en Swift no puedo trabajar con este tipo de matriz (he estado revisando documentación y foros y ejemplos en línea durante 24 horas ...)
¿Cómo recomendaría que persista guardar una matriz de objetos como el anterior? ¿O tal vez persistir guardar este tipo de matriz es una mala práctica?
¡Gracias de antemano por tu ayuda!
Agregar la clase de objeto:
class costCategory : NSObject {
var name : String
var defaultValue : Int
var thisMonthsEstimate : Int
var sumOfThisMonthsActuals : Int
var riskFactor : Float
var monthlyAverage : Float
init (name:String, defaultValue:Int, thisMonthsEstimate:Int, sumOfThisMonthsActuals:Int, riskFactor:Float, monthlyAverage:Float) {
self.name = name
self.defaultValue = defaultValue
self.thisMonthsEstimate = thisMonthsEstimate
self.sumOfThisMonthsActuals = sumOfThisMonthsActuals
self.riskFactor = riskFactor
self.monthlyAverage = monthlyAverage
}
}
Si intento guardar la matriz en NSUserDefaults, aparece el error:
Property list invalid for format: 200 (property lists cannot contain objects of type 'CFType')
He intentado usar la herencia de la clase NSCoder pero recibo un error que no puedo resolver, como se muestra a continuación:
class costCategory : NSObject, NSCoder {
var name : String
var defaultValue : Int
var thisMonthsEstimate : Int
var sumOfThisMonthsActuals : Int
var riskFactor : Float
var monthlyAverage : Float
init (name:String, defaultValue:Int, thisMonthsEstimate:Int, sumOfThisMonthsActuals:Int, riskFactor:Float, monthlyAverage:Float) {
self.name = name
self.defaultValue = defaultValue
self.thisMonthsEstimate = thisMonthsEstimate
self.sumOfThisMonthsActuals = sumOfThisMonthsActuals
self.riskFactor = riskFactor
self.monthlyAverage = monthlyAverage
}
// MARK: NSCoding
required convenience init(coder decoder: NSCoder) {
self.init() //Error here "missing argument for parameter name in call
self.name = decoder.decodeObjectForKey("name") as String
self.defaultValue = decoder.decodeIntegerForKey("defaultValue")
self.thisMonthsEstimate = decoder.decodeIntegerForKey("thisMonthsEstimate")
self.sumOfThisMonthsActuals = decoder.decodeIntegerForKey("sumOfThisMonthsActuals")
self.riskFactor = decoder.decodeFloatForKey("riskFactor")
self.monthlyAverage = decoder.decodeFloatForKey("monthlyAverage")
}
func encodeWithCoder(coder: NSCoder) {
coder.encodeObject(self.name, forKey: "name")
coder.encodeInt(Int32(self.defaultValue), forKey: "defaultValue")
coder.encodeInt(Int32(self.thisMonthsEstimate), forKey: "thisMonthsEstimate")
coder.encodeInt(Int32(self.sumOfThisMonthsActuals), forKey: "sumOfThisMonthsActuals")
coder.encodeFloat(self.riskFactor, forKey: "riskFactor")
coder.encodeFloat(self.monthlyAverage, forKey: "monthlyAverage")
}
}