简单的持久性存储斯威夫特 [英] Simple persistent storage in Swift
问题描述
我每个对象的数组与一些属性。这是通过对象的数组循环取一些样本数据:
名称=租金
默认值750
这个月估计= 750
则本月份统计报表= 0
风险因素= 0.0
月平均= 750.0
--------------
名称=票据
默认值250
这个月估计= 170
则本月份统计报表= 140
风险因素= 0.0
月平均= 190.0
--------------
名称=食品
默认值240
这个月估计= 200
则本月份统计报表= 95
风险因素= 0.0
月平均= 190.0
--------------
名称=午餐
默认值100
这个月估计= 150
则本月份统计报表= 155
风险因素= 0.899999976158142
月平均= 190.0
它的数据非常少,所以我要避免使用核心数据。我需要能够通过它来坚持将数组保存,然后再次打开它,并能够循环。我希望能像使用NSUserDefaults的或的NSKeyedArchiver但斯威夫特我可以既不用这种类型的数组运行的一个简单的解决方案(现在网上我已经经历文档和论坛和示例24小时...)
你会如何建议我坚持节约像上述对象的数组?或者,也许一直存在保存此类型的数组是不好的做法?
在此先感谢您的帮助!
添加对象类:
类costCategory:NSObject的{
变数名称:字符串
VAR设置defaultValue:诠释
VAR thisMonthsEstimate:诠释
VAR sumOfThisMonthsActuals:诠释
VAR riskFactor:浮法
VAR monthlyAverage:浮法
的init(名称:字符串,设置defaultValue:智力,thisMonthsEstimate:智力,sumOfThisMonthsActuals:智力,riskFactor:浮动,monthlyAverage:浮动){
self.name =名称
self.defaultValue =设置defaultValue
self.thisMonthsEstimate = thisMonthsEstimate
self.sumOfThisMonthsActuals = sumOfThisMonthsActuals
self.riskFactor = riskFactor
self.monthlyAverage = monthlyAverage
}}
如果我尝试保存阵列我NSUserDefaults的遇到了错误:
属性列表格式无效:200(属性列表不能包含类型'CFType'的对象)
我已经使用从NS codeR类继承尝试,但我得到一个错误,我不是能解决,如下:
类costCategory:NSObject的,NS $ C $ {CR
变数名称:字符串
VAR设置defaultValue:诠释
VAR thisMonthsEstimate:诠释
VAR sumOfThisMonthsActuals:诠释
VAR riskFactor:浮法
VAR monthlyAverage:浮法
的init(名称:字符串,设置defaultValue:智力,thisMonthsEstimate:智力,sumOfThisMonthsActuals:智力,riskFactor:浮动,monthlyAverage:浮动){
self.name =名称
self.defaultValue =设置defaultValue
self.thisMonthsEstimate = thisMonthsEstimate
self.sumOfThisMonthsActuals = sumOfThisMonthsActuals
self.riskFactor = riskFactor
self.monthlyAverage = monthlyAverage
} // MARK:NSCoding 需要方便的init(codeR德codeR:NS codeR){
self.init()在调用参数名称//这里的错误失踪的说法
self.name =去coder.de codeObjectForKey(名称)为String
self.defaultValue =去coder.de codeIntegerForKey(设置defaultValue)
self.thisMonthsEstimate =去coder.de codeIntegerForKey(thisMonthsEstimate)
self.sumOfThisMonthsActuals =去coder.de codeIntegerForKey(sumOfThisMonthsActuals)
self.riskFactor =去coder.de codeFloatForKey(riskFactor)
self.monthlyAverage =去coder.de codeFloatForKey(monthlyAverage) } FUNC EN codeWith codeR(codeR:NS codeR){
coder.en codeObject的(self.name,forKey:名称)
coder.en codeINT(的Int32(self.defaultValue),forKey:设置defaultValue)
coder.en codeINT(的Int32(self.thisMonthsEstimate),forKey:thisMonthsEstimate)
coder.en codeINT(的Int32(self.sumOfThisMonthsActuals),forKey:sumOfThisMonthsActuals)
coder.en codeFloat(self.riskFactor,forKey:riskFactor)
coder.en codeFloat(self.monthlyAverage,forKey:monthlyAverage) }
}
一种可能是你的对象属性转换:值字符串:对象将它们存储到 NSUserDefaults的
,然后获取和德code回来。
如果你想使用存储你的对象的NSKeyedArchiver
,你的类必须符合 NSCoding
并成为子 NSObject的
。例如:
类costCategory:NSObject的,NSCoding {
变数名称:字符串
VAR设置defaultValue:诠释
VAR thisMonthsEstimate:诠释
VAR sumOfThisMonthsActuals:诠释
VAR riskFactor:浮法
VAR monthlyAverage:浮法 的init(名称:字符串,设置defaultValue:智力,thisMonthsEstimate:智力,sumOfThisMonthsActuals:智力,riskFactor:浮动,monthlyAverage:浮动){
self.name =名称
self.defaultValue =设置defaultValue
self.thisMonthsEstimate = thisMonthsEstimate
self.sumOfThisMonthsActuals = sumOfThisMonthsActuals
self.riskFactor = riskFactor
self.monthlyAverage = monthlyAverage
} // MARK:NSCoding 所需的init(codeR德codeR:NS codeR){
//这里的错误在调用参数名称参数丢失
self.name =去coder.de codeObjectForKey(名称)为String
self.defaultValue =去coder.de codeIntegerForKey(设置defaultValue)
self.thisMonthsEstimate =去coder.de codeIntegerForKey(thisMonthsEstimate)
self.sumOfThisMonthsActuals =去coder.de codeIntegerForKey(sumOfThisMonthsActuals)
self.riskFactor =去coder.de codeFloatForKey(riskFactor)
self.monthlyAverage =去coder.de codeFloatForKey(monthlyAverage)
super.init()
} FUNC EN codeWith codeR(codeR:NS codeR){
coder.en codeObject的(self.name,forKey:名称)
coder.en codeINT(的Int32(self.defaultValue),forKey:设置defaultValue)
coder.en codeINT(的Int32(self.thisMonthsEstimate),forKey:thisMonthsEstimate)
coder.en codeINT(的Int32(self.sumOfThisMonthsActuals),forKey:sumOfThisMonthsActuals)
coder.en codeFloat(self.riskFactor,forKey:riskFactor)
coder.en codeFloat(self.monthlyAverage,forKey:monthlyAverage) }
}
然后就可以归档并保存到 NSDefaults
:
让默认值= NSUserDefaults.standardUserDefaults()
让arrayOfObjectsKey =arrayOfObjectsKey变种arrayOfObjects = [costCategory]()
VAR arrayOfObjectsData = NSKeyedArchiver.archivedDataWithRootObject(arrayOfObjects)defaults.setObject(arrayOfObjectsData,forKey:arrayOfObjectsKey)// ...VAR arrayOfObjectsUnarchivedData = defaults.dataForKey(arrayOfObjectsKey)!
VAR arrayOfObjectsUnarchived = NSKeyedUnarchiver.unarchiveObjectWithData(arrayOfObjectsUnarchivedData)为[costCategory]
I have an array of objects each with a number of properties. Here is some sample data taken by looping through the array of objects:
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
Its very little data so I want to avoid using core data. I need to be able to persist save the array then open it again and be able to loop through it. I was hoping to use a simple solution like NSUserDefaults or NSKeyedArchiver but in Swift I can get neither to work with this type of array (I've been going through documentation and forums and examples online for 24 hours now...)
How would you recommend I persist save an array of objects like the above? OR maybe persist saving this type of array is bad practice?
Thanks in advance for your help!
Adding the object class:
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
}
}
If I try to save the array to NSUserDefaults I'm getting the error:
Property list invalid for format: 200 (property lists cannot contain objects of type 'CFType')
I have tried using inheriting from the NSCoder class but I get an error which Im not able to resolve, as below:
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")
}
}
One possibility is to convert your object properties:values to string:object store them to NSUserDefaults
and then get and decode them back.
If you want to store your object using NSKeyedArchiver
, your class needs to conform to NSCoding
and be subclass of NSObject
. Example:
class costCategory : NSObject, NSCoding {
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 init(coder decoder: NSCoder) {
//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")
super.init()
}
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")
}
}
Then you can archive and save to NSDefaults
:
let defaults = NSUserDefaults.standardUserDefaults()
let arrayOfObjectsKey = "arrayOfObjectsKey"
var arrayOfObjects = [costCategory]()
var arrayOfObjectsData = NSKeyedArchiver.archivedDataWithRootObject(arrayOfObjects)
defaults.setObject(arrayOfObjectsData, forKey: arrayOfObjectsKey)
// ...
var arrayOfObjectsUnarchivedData = defaults.dataForKey(arrayOfObjectsKey)!
var arrayOfObjectsUnarchived = NSKeyedUnarchiver.unarchiveObjectWithData(arrayOfObjectsUnarchivedData) as [costCategory]
这篇关于简单的持久性存储斯威夫特的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!