English | 中文
The official inheritance extension for SmartCodable. Provides the @SmartSubclass macro that auto-generates CodingKeys, init(from:), encode(to:), and required init() for subclasses.
This macro depends on
swift-syntax(a large first-time download). Shipping it as a separate package keeps the core SmartCodable library lightweight for projects that don't need class inheritance.
Requirements: Swift 5.9+ / Xcode 15+.
// Package.swift
dependencies: [
.package(url: "https://github.com/iAmMccc/SmartCodableMacro.git", branch: "main")
],
targets: [
.target(
name: "YourApp",
dependencies: [
.product(name: "SmartCodableMacro", package: "SmartCodableMacro")
]
)
]SmartCodableMacro automatically depends on SmartCodable — no need to declare it separately.
import SmartCodable
import SmartCodableMacro
class BaseModel: SmartCodableX {
var name: String = ""
required init() {}
}
@SmartSubclass
class StudentModel: BaseModel {
var age: Int = 0
}At compile time, the macro auto-generates:
- A
CodingKeysenum (only the subclass's own fields) init(from: Decoder)— callssuperfirst, then decodes the subclass's fieldsencode(to: Encoder)— callssuperfirst, then encodes the subclass's fieldsrequired init()— only if the subclass doesn't define one
class BaseModel: SmartCodableX {
var name: String = ""
required init() {}
class func mappingForKey() -> [SmartKeyTransformer]? {
[CodingKeys.name <--- "stu_name"]
}
}
@SmartSubclass
class StudentModel: BaseModel {
var age: Int = 0
override static func mappingForKey() -> [SmartKeyTransformer]? {
let trans = [CodingKeys.age <--- "stu_age"]
if let superTrans = super.mappingForKey() {
return trans + superTrans
}
return trans
}
}The repo includes a full iOS demo project covering parent/child encode-decode, key/value mapping, and the didFinishMapping callback:
ExampleApp/ExampleApp.xcodeproj
Open it directly in Xcode. The project references this package locally via XCLocalSwiftPackageReference.
swift testIf you only need parsing (no inheritance), use SmartCodable directly — it's lighter and has no swift-syntax dependency.
MIT