Property wrapper in swift | Advance swift tutorial | Properties in swift 5.1

Property wrapper in swift | Advance swift tutorial | Properties in swift 5.1


Hey there welcome to another episode of iCoding with CodeCat this is Ravi, and in today’s session we are going to learn about property wrappers and this is just an introduction on property wrappers code used in today’s session it is already available in GitHub and google drive and if you want the code then what you can do is go into the video description and click on the Github and Google drive links you can click on the link and download the code for you one of the important functionality of registration screen was to make sure that the email id entered by the user to make sure that email id is valid and if it is invalid you prompt a message to the user to achieve this functionality my first thoughts are to create a string extension add a function inside the string extension and if the email id is valid this function returns true else this function returns false which has a function named validate and in this function, I am checking if the name and email id property of the structure is empty or not and let’s say if the email id is invalid definition of extension is if you define an extension of a particular type then throughout the application all the similar types can call the function declared in the extension if you are calling isValidEmail on the property email it makes more sense, but since over here you have used an extension the only reason is because I am using an extension and name property is also of string type The fact is I will never enter email id in the name property the name property will never call the isValidEmail string extension function I think if name will never call isValidEmail function then name property should not even access the isValidEmail extension function I cannot tell my extension that the isValidEmail function is only for the email property and not for every string property but since we have used extensions every string property throughout the application can easily access isValidEmail function To define property wrappers the syntax used will be you need an attribute name propertyWrapper and property wrapper can be used with struct, class, and enums in this particular example, I am going to make use structure property wrapper needs one more property internally which is wrappedValue we will use one more property which is going to be private I will be operating on this private property and the result of _value will be assigned to wrappedValue property before you return the anything from get but in this case, before we return any value we want to check whatever string value assigned to the _value property is a valid email or not? and for that what i need to do here is I will copy this function and paste it here and instead of using self here because in the case of extensions self means a string before i return in my get In property wrappers we need to know one thing and that’s the init method of property wrappers is private so I won’t define the init method yet as I want you to see the error that you will get here you see the syntax to declare a property wrapper here just the email property needs to implement the wrapper right in front of the property the init method of property wrapper is private by default and that’s the reason why you are getting this error the error here speaks that the initializer is inaccessible because it is private so what will happen here is when swift will try to get the value of the email property at first, what’s going to happen now is whatever the value assigned to email property is a valid email and if not then and if it’s a valid email id then return the value assigned to the property and if not then the wrapper will return an empty string the email id passed is valid so what happened here is when you passed an invalid email id over here where the validation is applied swift tried to get the value of the email and we all know that before get returns value our code runs which checks if the value assigned to this particular property is a valid email id or not where you use property wrappers in case of extension, there is not much code to do but in case of property wrappers, I have to write a little extra code but we don’t want such behavior just because in name property, you will never pass an email in this case, it made more sense that name should never even access isValidEmail and we isolated the email checking to one single property with the help of property wrapper if you have a new struct named Employee if you want to validate email id in what scenarios you have used property wrappers

11 thoughts on “Property wrapper in swift | Advance swift tutorial | Properties in swift 5.1

  1. A function should only be accessed by the property that needs it not by every single property of the entire application. With property wrapper, I can isolate my code to a single property and write code for only those property which needs it avoiding the use of extensions.
    Note: Property wrapper in swift is not a replacement for extensions they provide a layer of separation of the code that manages how a property value is stored by applying some rules to the value passed before it is stored in the property.

  2. Thanks for the video

    Helps in understand in detail regarding property wrapper

    Can we have video on custom framework sharing common code in app extensions and host app..?

  3. Thanks Man,
    i learnt two things in this one video,
    PropertyWrappers and when not to use extensions.. .Thanks a lot, and keep smiling…

  4. very good example. if we adopt it help us to control property accessibility and we can organized our code in better way …

  5. but why you declared wrappes valu le in structure.. while you do not use this variable anywhere.
    on thing more ..
    in email validation return with bool type .. not a string type. i think if you add bool rather than string … this example will be perfect.

  6. Other channels: this is the topic and this is the example and thank you for watching 🙄

    Codecat15: this is the topic,
    – clean code tips 🤩
    – no bookish examples 🤩
    – difference between old and new techniques🤩
    – focused on modernizing the code.🤩
    – Thank you for watching 👍 ☕️

  7. This video is just great and shows how well you know swift and clean code concepts, not only it tells why property wrapper is a great feature but the extension comparison was pretty good 👍 I don’t think any youtuber has ever explained like this ever.

    I am re-learning a lot of known concepts again thanks for this channel and my thinking process has changed after watching your videos and how you approach and write code 😎

  8. There is some passion, spark in u which is easily recognizable….what a explaination…..Humble request keep posting more videos on swift which vl be helpful for freshers like me…All his videos are mandatory for freshers in iOS…..Please need more videos on Autolayoutsss

Leave a Reply

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