SoFunction
Updated on 2025-04-10

Detailed explanation of the automatic archive and decomposition of NSCoding in swift

This article mainly introduces the automatic archiving and de-archiving of NSCoding in swift, which has certain reference value. Interested friends can refer to it.

middle

If there are fewer attributes, you can implement one attribute at a time, but if there are many, use runtime, and it is easy to realize the automatic archive and de-archiving of NSCoding.

Of course, we can directly call a macro definition of MJExtension and call the NSCoding proxy, which can be implemented in just one sentence.

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

The following is the source code

class BaseModel: NSObject,NSCoding{

  //The init method of the parent class must be implemented  override init() {
    ()
  }

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

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

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.