Property wrapper in iOS Swift

Updated 26 August 2021


According to swift doc “A property wrapper adds a layer of separation between code that manages how a property is stored and the code that defines a property”. In another way, it encapsulates read and write access and provides additional behavior to property.

By using the property wrapper we can reuse the code by applying it to multiple properties. It can be use with class, structure, and enumeration. For better understanding, we will take an example.

In the above example, we have created a structure name EmailValidationWrapper which has @propertyWrapper attribute. Here you have to define a wrappedValue property unless you will get an error like “Property wrapper type ‘EmailValidationWrapper’ does not contain a non-static property named ‘wrappedValue'”.

We have created a emailValidator method name “checkValidEmail” from getter we are calling this method. We have created another structure named Employee and for email validation, we have used our structure “EmailValidationWrapper”. And by using the property wrapper we have created our maintenance code and can be use multiple times.

Thanks for reading this blog.

For details, understanding you can visit the swift doc. You can also check other blogs from here. if you have any issues or suggestions you can leave your query/suggestion in the comment section.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*

Be the first to comment.

Start a Project

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home