Check Mobile App
Read now
Start a Project

Property wrapper in iOS Swift

Save

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.

css.php

When I think in getting solutions for my Ecommerce Platform, I think of Webkul. They have been such a blessing in our business. We have spent thousands of dollars in support and modules from them. We love their support team. They work very closely with us, and they always go the extra mile when we need help.

Michael Urresta

Michael Urresta

Talk to Sales

Global

Live Chat
Start a Project

    Message Sent!

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

    Back to Home