Reputation: 1233
I find myself spending a lot of time writing boilerplate constructors code like the following. Is there a way to make XCode generate this/some way to make this process faster?
public class T {
let t1: T1
let t2: T2
let t3: T3
public init(t1: T1, t2: T2, t3: T3) {
self.t1 = t1
self.t2 = t2
self.t3 = t3
}
Upvotes: 3
Views: 909
Reputation: 229
Sourcery is the great tool for that.
Here is template you can use (for sourcery version > 0.10):
{% for type in types.structs %}
{% if type|annotated:"AutoInit" %}
{% set spacing %}{% if type.parentName %} {% endif %}{% endset %}
{% map type.storedVariables into parameters using var %}{{ var.name }}: {{ var.typeName }}{% endmap %}
// sourcery:inline:auto:{{ type.name }}.AutoInit
{{spacing}} {{ type.accessLevel }} init({{ parameters|join:", " }}) { // swiftlint:disable:this line_length
{{spacing}} {% for variable in type.storedVariables %}
{{spacing}} self.{{ variable.name }} = {{ variable.name }}
{{spacing}} {% endfor %}
{{spacing}} }
// sourcery:end
{% endif %}
{% endfor %}
I got template from this AutoInit article
Upvotes: 2