Swift 中的简单持久存储 [英] Simple persistent storage in Swift
问题描述
我有一个对象数组,每个对象都有许多属性.以下是通过循环遍历对象数组获取的一些示例数据:
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
它的数据很少,所以我想避免使用核心数据.我需要能够持久保存数组然后再次打开它并能够循环遍历它.我希望使用像 NSUserDefaults 或 NSKeyedArchiver 这样的简单解决方案,但在 Swift 中我无法使用这种类型的数组(我已经在线浏览文档、论坛和示例 24 小时了...)
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?
预先感谢您的帮助!
添加对象类:
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
}
}
如果我尝试将数组保存到 NSUserDefaults,则会出现错误:
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')
我曾尝试使用从 NSCoder 类继承,但我收到一个我无法解决的错误,如下所示:
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")
}
}
推荐答案
一种可能性是将您的对象属性:值转换为字符串:对象将它们存储到 NSUserDefaults
然后获取并解码它们.
One possibility is to convert your object properties:values to string:object store them to NSUserDefaults
and then get and decode them back.
如果你想使用NSKeyedArchiver
存储你的对象,你的类需要符合NSCoding
并且是NSObject
的子类.示例:
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")
}
}
然后你可以存档并保存到NSDefaults
:
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]
这篇关于Swift 中的简单持久存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!