2

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)]=[]

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()

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)

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

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:

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

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

Thanks!

1
  • Really? Nobody can help to save an array of objects to NSUser defaults? I'd be happy with any suggestion even if they have nothing to do with NSUser defaults. Commented Oct 7, 2014 at 9:03

2 Answers 2

9

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
Sign up to request clarification or add additional context in comments.

Comments

-4

value of "financialData" should be in quotes:

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

1 Comment

Then would I not just be staring the string "costCategoryData" to NSUserDefaults instead of the array costCategoryData?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.