For better reading experience, please click

Take a look at the results first:

Before optimization:

After the optimization:

Template code
Sourcery
AutoUnbox
Unboxable
Codable

Step 1: KnowSourcery

Sourcery is a code generator for Swift language, built on top of Apple’s own SourceKit. It extends the language abstractions to allow you to generate boilerplate code automatically.

Sourcery is a Swift code generator based on Apple’s own SourceKit. It achieves automatic generation of template code by extending the abstraction of the language.

Step 2: Write the template

Create an identified tag in the project:

Font language

Step 3: Introduce Sourcery to Podfile

  • pod ‘Sourcery’
  • pod install

Step 4: Add Script

Add a Script to the Build Phases of the project named Sourcery. $PODS_ROOT/Sourcery/bin/sourcery –sources $SRCROOT/creams –templates $SRCROOT/creams/Infrastructure/Sourcery/AutoUnbox/AutoUnbox.stencil –output $SRCROOT/creams/Infrastructure/Sourcery/ Note that Sourcery is first to ensure that the template code is generated before the code is compiled

test

  • comand + B
  • In the $SRCROOT/creams/Infrastructure/Sourcery/path will be c generates a AutoUnbox generated. Swift, this file contains all of the generated code, That is, all extensions that need to implement Unboxable models.