Mutating Functions Swift | Why we use Mutating Functions ?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to the channel in this video we're gonna talk about mutating functions in swift in the most easiest possible way so without any further ado let's get started with our xcode [Music] so guys as we already know that classes are reference type whereas the structs and enums are of value types in swift so what do i mean by that is class object shares a single instance of the object and passes the same reference if passed to any function or new object whereas the value type is the one which creates a copy of it and passes only the value right so for that we have an example right here we have this class called rectangle which has two properties width and height and we have a function that is called area which returns as the area of this particular rectangle and doesn't modify anything inside this function we just try to multiply the width and height and return that value in the second function we have you know this parameter called value and we try to modify our width and height according to the value that we passed through this function called scale by all right now if i try to you know make an object of this class let's create an object now i'll try to you know return the area of this class if i try to run this it will simply return 2 so guys as you can see we have 2 right here which is very obvious now if i try to scale this rectangle by some value so i'll try to scale it by three suppose now if i try to print the area sorry for writing react again and again so now i'll try to print the area again so it would be 18 this time because we have multiplied both the values by 3 and now after that we have this area function which multiplies both of those values which we have modified just before this area command so now we have this result called 18. now if i apply the same case to a structure which is a value type so let me just replace class with the structure all right now this rectangle is a struct and we have again these two functions first one is the area and as you can rightly see we have some errors in our second function in which we are trying to modify the properties of our structure so the error is saying that the left side of the mutating operator is not mutable self is immutable so consider this as self dot width and self dot height so the error is simply saying that anything which which is related to self the there we have the width and height which are inside self which are inside our rectangle i mean the self rightly points to our you know structure which is rectangle and it does not allow anything to get modified inside it that is what the error is saying to us now the mutating keywords comes into the picture and if i write mutating function and the same thing like before for a doubt i can just remove this self if you think that it has changed anything no i just wrote it to make you understand that what self is basically mean now you can see that we don't have any errors right here right but we do have an error uh below in the code so this error says that cannot use mutating member on immutable value rect is a lag constant as you can rightly see here rect is a lag constant which is right we cannot modify a let constant in the case of a value type be it a variable value inside the structure but if you have made an object as a lag constant it would not allow you to make the changes because remember this always values never change variables do okay i can repeat that values never change variables do so if i convert this to a variable now there would be no errors as you can rightly see the error will be yes it's gone now if i try to run this it will work perfectly fine as you can see right here we have 18 again which was there in the case of class as well we have two which was basically when we never made any changes and after making the changes we have this 18 okay so guys this is how mutating function is working mutating function is simply doing one thing which is allowing the self to mutate which is by default immutable in the case of a value type you can take a case of a structure you can take this case of enum you would see that the self is by default immutable in value types so guys that's all about mutating function if you like this video please give it a big thumbs up and subscribe to my channel for more such videos in future see you in the next one bye bye jai hind you
Info
Channel: Mayank Gupta
Views: 1,113
Rating: undefined out of 5
Keywords: mutating functions, mutating function swift, mutating function in swift, why we use mutating functions in swift, where we use mutating function in swift, swift mutating functions, mutating functions in swift ios, what are mutating functions in swift, value types and mutating functions, swift reference and value types, struct vs class swift, mutating keyword in swift, mutating methods in swift, what are mutating methods, where we use mutating methods in swift, swift tutorials
Id: ko_6hq2-PG4
Channel Id: undefined
Length: 5min 15sec (315 seconds)
Published: Wed Nov 04 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.