iOS Development | Intro to Property Wrappers in SWIFT

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone and welcome back to my channel i'm shefaly and you're watching swiftly Chevalley and today we're talking about properties in Swift and specifically property rappers property rappers are growing in popularity for iOS developers after the introduction of Swift UI you will notice that in all Swift UI code we use a new @ symbol to notate a property rapper so much so that the main property rapper was introduced this year at WWDC 20 and that's all we need in order to start an app with Xcode 12 and iOS 14 so if you're familiar with Android apps they have something very similar called dagger in any case this is a very powerful addition to Swift so let's jump right into the code so before we get started with property wrappers I want to go ahead and recap some of the basic functionalities that properties already have so properties are values that are associated with a particular class structure or you know so in this case as you can see on my screen I have a struct called suitcase and it has two properties one is color and one is weight so these are known as stored properties which are stored values that are part of an instance we can also have something called a computed property which is calculating any value as opposed to just simply storing it let's see an example of this here in the second structure that I've created I have three properties passenger name two possible bags which are type suitcase and I'm going to add a fourth property here which is not stored it's going to be computed and we will call that variable is total weight acceptable and it's going to be a boolean so true or false and what it's going to do is compute the value so we can go ahead and explicitly create a getter over here and we'll say let total equals bag one weight plus bag two weights and then return that total with a computation here that says if it's less than or equal to fifty pounds and over here we can see that this value is not necessarily just stored but every time we want to access it we're going to be doing this kind of computation so those are the two basic types of properties that one can have in Swift and then also another thing that we can do with these properties is obviously to get and set them so if we create a a new bag which is of type suitcase and let's give it black color and 25 pounds then with our properties we can go ahead and say bag color and so we're getting that property it's black we can also set it to something else if we can set it to blue so that's setting it you can't set a property if the variable within it is let so that means it's a constant so in this case we're not able to set the color what we could do is correct this and say variable but in real life it's not possible to you know change the color of the suitcase so we're going to leave that a set and that means it's going to be get only another thing that you can do with properties is to define a property observer this is to monitor the change in a property's value and so two different property observers that you can use is will set and did set so for example if we have a ticket and we have a passenger name we could create a observer with it saying like we'll set this property and before setting it this is called before setting at the property and we can ask our customers if they want to go ahead if they want to be certain that they want to set it so we can put in something like about to set the name of a passenger please confirm or something like that and then we can also have another observer called dead set and what didset does is it will print out after the property is actually set so we'll just say something like congrats you have successfully changed the name of the passenger so let's go ahead and see this in action we can go ahead and create a new ticket so we can say like ticket to Paris and equal to a new ticket and we have a passenger and let's just say it's me that's going on this trip then we have bag I have this bag and let's go ahead and create a nother suitcase here blue and this one is like 45 pounds so over here we have ticket and now let's go ahead and change the value of the passenger name suppose I can't make my trip anymore and I want to transfer my ticket to someone else so let's go ahead and say ticket to Paris and the name I want to change it to my friend Sally and so first it will say about $0.02 the name of the passenger please confirm and then congrats you've successfully changed the name now we can also set a condition within the didset to say if the passenger name is not the same as the old value then it's the only way that we'd successfully change the name if you're trying to assign the ticket to the same passenger then that's not a valid case so we can go ahead and say if passenger name does not equal to old value and old value is a key word that basically it keeps track of the old value that we were currently working with so over here so now if the passenger is now Sally and we want to again try and change the passenger name to Sally again for some reason because we forgot we already did this what's going to happen it's about to change the name of the passenger please confirm Congrats we've already changed it because we changed it from Shivani to Sally and now we're attempting to change it for the third time and/or for the second time and it says about to change the name of your passenger please confirm and over here we'll see that it did not forget successfully changed because we put in this condition and will confirm that oh this is not a valid case so we're not going to do this so those are some of the basic functionalities that you can do with properties and now we can introduce property wrappers so property wrappers add an additional layer of separation between the code that manages how a property is stored and the code that defines the property so when you use a property wrapper you make a struct in amor class that defines a wrapped value property so let's see this in example and I will go ahead and create a new property up here for the example of property wrapper and over here we'll define a struct and we'll call it 50 or less and what I'll do is use this @ symbol as a notation I'll say at property wrapper and over here and then I have a struct and what this will do is it will have a private stored property called weight so this will this will keep track of the weight in each of the suitcases that I want to take so they don't get clarified a little bit more in the lesson but over here I'll create an initializer and I'll first set the weight equal to 0 over here you can see that this error is saying that we need a wrapped in value so we'll go ahead and add that here and call it I mean it has to make all wrapped value and it's going to be of type int because it's going to wrap the value of the weight and we'll set the gutter in the center here and it's going to return the weight if we want to get it but if we want to set it this is where we're going to put our condition so we'll say weight is equal to either 50 pounds or less so either the new value or 50 pounds pick the minimum so that this way we won't be able to create any anything with a weight that is higher than 50 so over here we can see that we have 50 or less and what we can do is we can limit the suitcase to have a weight of 50 or less because our example over here when we gave us the total weight acceptable has to be less than 50 pounds but that doesn't really make sense with the real requirements that airlines have it's really you know 50 pounds per bag that you take not 50 pounds total and so how do we go ahead and account for this and that's where this property wrapper will come in so we'll use this at sign and we'll say at 50 or less over here and we have wrapped or the stored property weight within suitcase with 50 or less that means that anytime we want to assign a a value to weight it is going to be 50 or less because it adheres to this same is this computation and this property so we can go ahead and keep this in an example and since we've already added our condition here you know we can go ahead and remove this value and let's create a new bag over here large suitcase purple and let's try and set the weight to be something extremely large like 75 but that does not adhere to the Airlines restrictions so let's see what actually happens when we try and check in this bag the weight is going to be reduced to 50 pounds even we tried to check in a bag with 75 pounds because we have this proper you wrapper it's going to when it goes into the setter it's going to pick the minimum between 75 or 50 and always return 50 so you can use the property wrapper over and over again throughout your codebase if you have something that needs to conform to some kind of requirement like this so any struct that I can create that needs this 50 year less requirement any property that I want I can make it adhere to this property wrapper and that's really cool and powerful I suppose a maximum capacity on some kind of like elevator or something the items that you put on there have to be 50 pounds or less you can go ahead and use reuse this same property wrapper over and over again throughout your codebase and you don't have to do that logic again and again so if this makes sense to you if you understand the use of property wrappers that then give this video a thumbs up don't forget to subscribe to my channel and continue watching swiftly Chevalley
Info
Channel: Swiftly Shivali
Views: 1,736
Rating: undefined out of 5
Keywords: Swift, iOS, Programming, apps, mobiledevelopment, Enum, SwitchStatement, xcode, ios developer, swift programming, swift tutorial, ios development, how to make an app, learn swift, ios swift, swift 5, rawvalue, raw, value, Window, Storyboard, view, ARC, Automatic Reference Count, Weak, Strong, Class, Memory, Management, SwiftUI, Properties, Property, Property Wrapper, Swift 5, Learn Swift, @main
Id: lxdSiq8drXQ
Channel Id: undefined
Length: 14min 37sec (877 seconds)
Published: Fri Jun 26 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.