Property Wrappers in Swift
tips
language
Property wrappers provide a way to add behaviors like validation, transformation, or default values to properties without repeating the same code elsewhere.
Let’s take an example where we can use property wrapper for performing a transformation on a value.
We will create a @Capitalized
property wrapper that automatically capitalizes any string assigned to
a property:
The setter in the above example takes the incoming value and capitalizes it and assigns it to the value property. The getter, as we can see then provides the capitalized value.
You can now use @Capitalized
to automatically capitalize user input:
As we can see, in the above example the name gets capitalized everytime behind the scenes when the name is set.
Sponsored