0

Swift 2 Xcoode 7.3

I try to store this:

var someArray = [Class1(id: 1, titel: "Titel 2", something: Class2(somevar: 20))]

with NSUSerDefaults:

let arrayData = NSKeyedArchiver.archivedDataWithRootObject(someArray) NSUserDefaults.standardUserDefaults().setObject(arrayData, forKey: "array")

My classes look like this:

class Class1 {

var id: Int
var titel: String!
var something: Class2



init(id: Int, titel: String, something: Class2) {
    self.id = id
    self.titel = titel
    self.something = something

}

required init(coder aDecoder: NSCoder) {
    self.id = aDecoder.decodeIntForKey("id")
    self.titel = aDecoder.decodeStringForKey("titel")
    ???
}

func encodeWithCoder(aCoder: NSCoder) {
    aCoder.encodeObject(id, forKey: "id")
    aCoder.encodeObject(titel, forKey: "titel")
???
}


class Class2: Class1
{

var somevar: Int

init(setback: Int)  {
    self.somevar = somevar
}

}

What do I need to add in those classes?

(Have to add some mor details; but I think it's self-explaining)

2 Answers 2

1

Uses @FLX code sample and ensure every object in your object's tree also conforms to NSCoding.

Sign up to request clarification or add additional context in comments.

Comments

0

it's very easy.

write:

self.something = aDecoder.decodeObjectForKey("something") as! Class2

read:

aCoder.encodeObject(something, forKey: "something")

Comments

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.