0

I was going to use Core Data but thought this was easier but can't find anything online about how to store an array of Cell class as a variable in the Cell class itself.

class Cell: NSObject, NSCoding {
  var name:String!
  var tag:String?
  var more:String?
  var amount:String!
  var balance:String!
  var subCells:[Cell]?//THIS IS THE ONE I'M STUGGLING WITH

  override init() {
    super.init()
  }

  init(name: String, tag: String, more: String, amount: String, balance: String, subCells: [Cell] = [Cell]()) {
    super.init()
    self.name = name
    self.tag = tag
    self.more = more
    self.amount = amount
    self.balance = balance
    self.subCells = subCells//THIS
  }

  required init(coder decoder: NSCoder) {
    self.name = decoder.decodeObject(forKey: "name") as! String
    self.tag = decoder.decodeObject(forKey: "tag") as? String
    self.more = decoder.decodeObject(forKey: "more") as? String
    self.amount = decoder.decodeObject(forKey: "amount") as! String
    self.balance = decoder.decodeObject(forKey: "balance") as! String
    self.subCells = decoder.decodeObject(forKey: "subCells") as? [Cell]//THIS

  }

  func initWithCoder(decoder: NSCoder) -> Cell{
    self.name = decoder.decodeObject(forKey: "name") as! String
    self.tag = decoder.decodeObject(forKey: "tag") as? String
    self.more = decoder.decodeObject(forKey: "more") as? String
    self.amount = decoder.decodeObject(forKey: "amount") as! String
    self.balance = decoder.decodeObject(forKey: "balance") as! String
    self.subCells = decoder.decodeObject(forKey: "subCells") as? [Cell]//THIS
    return self
  }

  func encode(with aCoder: NSCoder) {      
    aCoder.encode(self.name, forKey: "name")
    aCoder.encode(self.tag, forKey: "tag")
    aCoder.encode(self.more, forKey: "more")
    aCoder.encode(self.amount, forKey: "amount")
    aCoder.encode(self.balance, forKey: "balance")
    aCoder.encode(self.subCells, forKey: "subCell")//THIS
  }  
}

Am I forced to use core data to solve this or can I encode the array before encoding the whole class in the viewdidload()

3
  • FYI - You don't need the initWithCoder(decoder:) method, just the init(coder:). Commented Dec 31, 2017 at 7:29
  • 1
    What problem or error message is you have with your current code? Commented Dec 31, 2017 at 8:16
  • NS error, I fixed it by encoding and decoding before again, kinda recursive Commented Dec 31, 2017 at 22:53

1 Answer 1

1

ok i figured it out

required init(coder decoder: NSCoder) {
    self.name = decoder.decodeObject(forKey: "name") as! String
    self.tag = decoder.decodeObject(forKey: "tag") as? String
    self.more = decoder.decodeObject(forKey: "more") as? String
    self.amount = decoder.decodeObject(forKey: "amount") as! String
    self.balance = decoder.decodeObject(forKey: "balance") as! String
    self.subCells = NSKeyedUnarchiver.unarchiveObject(with: (decoder.decodeObject(forKey: "subCells") as! NSData) as Data) as? [Cell]
}


func encode(with aCoder: NSCoder) {

    aCoder.encode(self.name, forKey: "name")
    aCoder.encode(self.tag, forKey: "tag")
    aCoder.encode(self.more, forKey: "more")
    aCoder.encode(self.amount, forKey: "amount")
    aCoder.encode(self.balance, forKey: "balance")
    aCoder.encode(NSKeyedArchiver.archivedData(withRootObject: self.subCells), forKey: "subCells")

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

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.