In swift NSCoding is automatically archived and unarchived

  • 2020-05-19 06:02:25
  • OfStack

This article mainly introduces the automatic archiving and unarchiving of NSCoding in swift, which has a definite reference value, and those who are interested can refer to it for 1.

1. OC

If there are fewer attributes, one attribute can be implemented, but if there are more, runtime can be used to realize the automatic archiving and file reconciliation of NSCoding.

We can, of course, call one of MJExtension's macro definitions directly and call the NSCoding proxy in just one sentence.

2.swift

We can't call MJExtension's macro definition, but we can call MJExtension to implement the method inside:

Here is the source code


class BaseModel: NSObject,NSCoding{

  // Of the parent class init Methods must be implemented 
  override init() {
    super.init()
  }

  required convenience init?(coder aDecoder: NSCoder) {
    self.init()
    self.mj_decode(aDecoder)
  }

  func encode(with aCoder: NSCoder) {
    self.mj_encode(aCoder)
  }
}


Related articles: