斯威夫特转换为数组为NSData的持续NSUserDefaults.StandardUserDefaults存储 [英] Converting Swift Array to NSData for NSUserDefaults.StandardUserDefaults persistent storage

查看:202
本文介绍了斯威夫特转换为数组为NSData的持续NSUserDefaults.StandardUserDefaults存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的周围斯威夫特头通过一个小的应用程序(是与对象 - 相对胜任后)。我想NSUserDefaults的使用持久保存的数据量小,但我有问题。

I'm trying to get my head around Swift (after being relatively competent with Obj-C) by making a small app. I would like to use NSUserDefaults to persistently save a small amount of data but I am having problems.

我初始化的元组空数组是这样的:

I initialise an empty array of tuples like this:

var costCategoryArray: [(name:String, defaultValue:Int, thisMonthsEstimate:Int, sumOfThisMonthsActuals:Int, riskFactor:Float, monthlyAverage:Float)]=[]

在该数组有一个入口,我要救阵与标准斯威夫特code等NSUserDefaults的,因为这:

When the array has an entry, I want to save the array to NSUserDefaults with standard Swift code such as this:

NSUserDefaults.standardUserDefaults().setObject(costCategoryArray, forKey: "financialData")
NSUserDefaults.standardUserDefaults().synchronize()

我得到一个错误说,元组不符合AnyObject类。所以我试图把它变成NSData的:

I get an error saying that the tuple array doesn't conform to the AnyObject class. So I tried to turn it into NSData:

var myNSData: NSData = NSKeyedArchiver.archivedDataWithRootObject(costCategoryArray)
var myUnarchivedData: Array = NSKeyedUnarchiver.unarchiveObjectWithData(myNSData)

...但我转换为NSData的过程中得到了同样的错误。对象正在举行的我的数组不符合AnyObject。我也试着在每一个阶段,使之不可改变使用:

...but I get the same error during the conversion to NSData. The object being held by my array doesn't conform to AnyObject. I've also tried at each stage to make it immutable by using:

let immutableArray = costCategoryArray

香港专业教育学院还试图创建一个类,而不是使用我明白这将使它符合AnyObject元组:

Ive also tried creating a class instead of using tuples which I understood would make it comply with AnyObject:

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
    }
}

但是,新的错误是:

But the new error is:

属性列表格式无效:200(属性列表不能包含类型'CFType'的对象)

"Property list invalid for format: 200 (property lists cannot contain objects of type 'CFType')"

什么是元组数组的问题?为什么我不能存储类对象的数组?我觉得我需要一些专家的意见,到目前为止一切我试图用斯威夫特做的是pretty很多不兼容的...

What is the problem with an array of tuples? Why can't I store an array of class objects? I feel like I need some expert advice as so far everything I try to do with Swift is pretty much incompatible...

谢谢!

推荐答案

任何你正在归档至的NSData和背部需要实现NSCoding协议。我发现,除了我的雨燕类不得不延长NSObject的。这里是一个斯威夫特类的一个简单的例子是连接codeS和德codeS:

Anything you are archiving to NSData and back needs to implement the NSCoding protocol. I found that in addition, my Swift class had to extend NSObject. Here is a quick example of a Swift class that encodes and decodes:

class B : NSObject, NSCoding {
    var str : String = "test"

    required init(coder aDecoder: NSCoder) {
        str = aDecoder.decodeObjectForKey("str") as String
    }

    override init() {
    }

    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(str, forKey: "str")
    }
}

// create an Object of Class B
var b : B = B()

// Archive it to NSData
var data : NSData = NSKeyedArchiver.archivedDataWithRootObject(b)

// Create a new object of Class B from the data
var b2 : B = NSKeyedUnarchiver.unarchiveObjectWithData(data) as B

这篇关于斯威夫特转换为数组为NSData的持续NSUserDefaults.StandardUserDefaults存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆