Computed Properties

Last updated 2 months ago

Computed properties are special properties that do not come from the Datafile. Instead they are computed from other properties and used inside our templates.

Let's add a computed Property inside our Pluginfile.

name: MyFirstPlugin
isTemplate: true
properties:
- name|String
- age|Int!|18
computedProperties:
- desc|String

Now run the command sh scripts/run.

When you open the file MyFirstPlugin.swif, you will see that a template placeholder for computed property is added for you.

public class MyFirstPluginModel: Codable {
public var name: String
public var age: Int!
‚Äč
public var desc: String {
/// <aarkay desc>
/// your code goes here.
/// </aarkay>
}
...
}

You can you use your custom logic to compute the value of the desc.

public var desc: String {
/// <aarkay desc>
return "\(name) is \(age) years old!"
/// </aarkay>
}

You can start using {{ desc }} inside your Templatefile now.