Why property Wrappers are so important in Swift?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys welcome to another video some time ago we talked about estate management in swift ui in those videos we saw things like state estate object observe object all of them are property wrappers included in swift ui but also swift in 5.1 included property wrappers for us to build our own custom property wrappers so we will talk about that however i wanted to keep this video simple so today we will talk about the basics of how to build a property wrapper and in the next video we will talk more about other details limitation and how to combine multiple property wrappers let's see if that's possible in the next one but today let's start since the beginning my name is pete and this this is swift tips all right let's start but before continuing just let me clarify why we need property wrappers in first place well property wrappers help us to encapsulate a lot of business logic in one place and then apply it to any particular property that we require it's really helpful and in that way we could avoid to have multiple managers or other code that maybe we just care about specific cases for properties but it's around because we need it as a helper method or things like that how many of you have utilities of helper folder in your applications yeah so that kind of things maybe could fit with property wrappers but anyway let's work on our demo for this demo in particular i want to explain in detail all the components to build a property wrapper maybe the example it's not so useful but it could give you the idea and later we will see more real examples applying all the knowledge here okay so then let's start we have here an extract user and a couple of properties email age first name and last name all good nothing serious here however when we start initializing this user we notice that we are initializing h as -1 and yeah that's an error right there's no make sense at all and if we try to print this yeah we got a minus one which is totally incorrect okay then what is the goal for this property wrapper demo we want to limit the value of range to a range i want to for example if we got numbers below then 0 which is the minimum h you can get in years then for example if we got minus 1 or less we could establish 0 as the limit anything less than zero will be zero and at the same time uh max limit so for example if we try to use an age of thousand perfectly thousand years maybe that's not good right because it's impossible otherwise let me know in the comments how to live thousand years please but here what we want to validate is then if we got a number greater than 120 we will get 120 has the maximum value so our range will be between 0 and 120 so that makes more sense for an h value so okay let's start now we have the problem then let's work on building or property wrapper okay what we need to do well a property wrapper could be built basically with any type what i mean is that you can use an instruct and a gnome or a class it will depends of your requirements in this case let's use an extract so i'm going to build an extract age because i will mark my property in this case h which is type int to the property wrapper h however yeah is this just a simple struct how can we then transform this struct into a property wrapper it's really simple we need to use an attribute from swift that is called property wrapper automatically now we are telling swift that this struck is not a regular stock but is a property wrapper that will be applied to any type that fit the requirements of this struct you will see that in a moment by the way if you don't know what is an attribute it's really simple um just extensions to you know capabilities that tell swift more information about this one example of other attributes in swift is the well-known objective-c to communicate with objective-c logic but anyway we'll talk more about that later all right we have here property wrapper h however we need to explicitly create a variable wrap value here that's required to build a property wrapper with this attribute so okay let's then create a property wrapper and use equal to zero okay compiler is happy now okay and what is this wrap value basically this rap value will expose what we wrap in this property wrapper since that we want to wrap h so in this case rap value will wrap h value and if h is of type int rap value requires to be into now how we gonna initialize this rap value with age well it's really simple it's like a regular struct you have initializer so we need to create an initializer but in this case we have an special initializer well not a special but let's see this as a parameter you need to explicitly say that you are using a wrap value this will help swift to make the relation between the value you want to wrap and the property wrapper you create so in this case rap value is end oops and then we can just say self-rap value is equal to rap value can we apply now or property wrapper let's see that just simple with this annotation and usage and yeah it's working let's try that it's cool however yeah this is redundant we are not doing basically nothing with rh by the way if you wonder why this is working because we add this raw value we but we are not using it here well this is kind of magic because swift is inferring couple of things and it's transforming your declaration with in this case uh you don't have default values here but you could also add for example zero here and this technically is equivalent to this if you wonder why we have this error this is because well strucks have a member wise initializer and this is not a fit anymore with what we have here because internally this is now uh h value because well we're wrapping up but let me just remove this then boom it's working so yeah this thing is equivalent to this but i think you will prefer to use this one so let's keep it for now but yeah as you can see the magic internally here about using property wrappers is made by swift so basically you cannot do anything else and use this h as a regular value in this case int and annotate your age property with the age property wrapper okay but let's go back to the problem yeah again we are not doing anything yet how we're gonna validate or input okay let's see first here if we can do it so what we want to restrict here is the usage of outside of the range so let's try to check this rap value for example we want to validate the minimum value a minimum value possible should be zero if it's a minus one minus thousand etc what we need here is just then use get the max value between zero and rapalje because if rap value is greater or equal to zero yeah we will get that number otherwise if we are inserting here minus 3 we will get 0 because 0 is greater than minus 3. that's really easy now let's do the same but now with the upper range so for the other one maybe it's obvious let's use a mini value so the mean value here is 120 and the result of this other validation that's cool right that's all that we have to do i think so let's see so for example let me just remove this and then bring back or age and or minus one let's see what happen yeah it's zero even that i am explicitly saying minus one let's try minus thousand well ten thousand it's still zero now what about thousand yeah it's in the limit cool however let me see if this is working with h equal to i don't know um 700 look at that i am getting the 700 but this is happening because we are only validating this in the initialization but if someone else wants to modify or age value well we will have an issue yeah i know we can protect this age to be i don't know a private set for modification but let me just show you that yeah in this case we are using rap value has a store property but also you can use it as a computer property in some way depending of other value let me show you that so instead of doing this transformation here let's add another private variable that will contain the actual value of h instead of doing rap value here so rap value then will be just the provider of whatever private value contains and that's it it will be really straightforward the only thing we need to do is then declare a private bar value and let's call something h i hope you don't confuse with the other h okay now instead of having this declaration here let me move this below and what i want is yeah having we you need mandatory wrap value here but what i want is use some getter and setter explicitly and then wrap the h value to whatever we want here for the case of get i want just to return whatever is in h okay but in the case of set which means when we establish an initialization like this one we want them to modify h and to do that what we need to do is just call h equal to and now here we will need to replace this value here okay this this validation so let me bring back this here but now instead of calling rap value set contains something called new value that makes reference to the value that is about to be assigned to this variable in this example new value could be 700 that's it now instead of having this here because i don't want to have the same logic twice because it's not good for you know many reasons we just need to use rap value but the magic here is that since that we are calling rob value our value will trigger this set and then the set will initialize and assign h properly that's it let me run this again wow it's working let's try just a few more examples nice it's still in the range now what will happen if we insert a regular value for example 32 cool is working yeah in this case we are in the range so yeah the value is what it is pretty nice yeah i know this example is pretty steep forward but again the point of this is to show you the things that you need to do to build your property wrapper you need to decorate this struct whatever you call it with attribute property wrapper you need to establish a rough value no matter if it's a store value or computer property like this one but you need to specifically mention in your struct that you are using wrap value because it's the connection to swift between your actual property you want to wrap and the property wrapper and yeah if you need helper variables here like this one yeah is that possible and also you could use initializers that make reference to wrap value and then swift can infer everything for you and you don't need to use you can use this format if you want but internally by default you can use your property as a regular property and just decorating with this h property wrapper that we just made this is cool but let me just add one more thing before finish this demo we explicitly hard code or range to 0 to 120 but maybe someone could say no no i want to increase or modify my upper range because well i am a vampire i can leave more than that that's fine so you can also add other parameters to your initializer let me show you that we want a max limit uh different for for this value so let me create a variable to just store that information i don't know let's set by default 120 but here let's add a new parameter in the initializer max h makes sense and now the only thing we need to do is just use mac h equal to max h and now instead of using 120 directly we need to use max h actually let me put this 120 here and then use this value here compiler is happy now cool you can still using your property wrapper as it is but also here you can use your max value age i don't know 150 however here you require to now explicitly say default value for your initializer you have two choices you can explicitly say equal to zero and everything will be ready or and you can use here graph value equal to zero or whatever you want so it's up to you to decide what what will be the best but yeah uh this format is having issues so yeah i prefer just to keep this one has this default initialization all right let's try now or work yeah it's working because we are not breaking or rule still default 120 but now we are saying that or max h possible is 150. so as you can see you can use additional parameters in your property wrappers and well the sky's the limit about what you can do with this powerful tool that's it for this video we talk so much about rough value because it's the heart of the property wrappers and it's good to you the reason why we need property wrappers in first place yeah this example was really simple but believe me in the next one we will see more about how to build more complex property wrappers and also use your imagination to build your own property wrappers let me know in the comments what kind of things we like to build with property wrappers i would like to see your comments also don't forget to subscribe and leave your likes if this video was useful for you thank you so much and have a great day you
Info
Channel: Swift and Tips
Views: 261
Rating: undefined out of 5
Keywords: swift and tips, swift && tips, swiftandtips, pitt500, Pitt, Pedro Rojas, Swift, SwiftUI, ios, ios development, Property Wrappers, wrappedValue, demo, struct, computed properties, Swift 5.1, State management, State, StateObject, swiftlang
Id: cCOHJkd3kBE
Channel Id: undefined
Length: 17min 47sec (1067 seconds)
Published: Sun May 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.