Copy on write & Copy on assignment in Swift | Hindi tutorial

Поділитися
Вставка
  • Опубліковано 2 сер 2024
  • swift uses copy on write technique with value types like arrays to manage the memory better, this is one technique used where value types are stopped from polluting the memory by creating multiple instances of it. Not all value types in swift perform copy on write operations but they do perform something called as copy on assignment.
    Given are the chapters for today's video
    0:00 introduction
    0:36 reference type behavior
    3:09 value type behavior
    5:11 copy on assignment
    5:50 copy on assignment problem with arrays
    7:27 copy on write code example
    10:34 interview tips
    11:07 conclusion
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ✅ Subscribe to the channel and stay ahead of everyone by following the right way for writing clean code in swift. 🔥🔥🔥🔥🔥
    🔗Click on the link to subscribe: ua-cam.com/users/codecat?sub...
    iOS Cafe discord server invite link: / discord
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ############################################
    Connect with me on social media
    Instagram: codecat15
    📧: codecat15@gmail.com
    🐦: / codecat15
    Facebook: / code.cat.792
    Github: github.com/codecat15
    ###########################################
    #copyOnWrite #valueTypevsReferenceType #swifttutorial

КОМЕНТАРІ • 45

  • @aarukiapniduniya
    @aarukiapniduniya 2 роки тому +2

    Amazing topic to discover.... Great work Ravi ....

    • @CodeCat15
      @CodeCat15  2 роки тому

      Pretty interesting how such minor details are being taken care of when it comes to memory management, indeed this topic is very interesting and an interviewer favorite question.

  • @narayanrautela5714
    @narayanrautela5714 2 роки тому +2

    For let and var question, I think we have to change let to var because struct are immutable type so we need to change it.
    Like we use mutating in stuct for modifications.
    Thanks bro for this video 👍

    • @CodeCat15
      @CodeCat15  2 роки тому +1

      Excellent explanation Narayan 👍 thank you for your answer

  • @JunaidKhan-ny8tu
    @JunaidKhan-ny8tu 2 роки тому +1

    This is amazing Ravi Bhai ❤❤❤ very concise and to the point 👏

    • @CodeCat15
      @CodeCat15  2 роки тому +1

      Thanks Junaid, I am glad this was helpful session.

  • @arifashraf8342
    @arifashraf8342 2 роки тому +1

    Great explanation as usual!

    • @CodeCat15
      @CodeCat15  2 роки тому

      Thank you Arif, am glad it was helpful

  • @nripendrasingh7472
    @nripendrasingh7472 2 роки тому +1

    Hi Bro, Amazing explanation, you are providing very valuable knowledge. Keep it up !

    • @CodeCat15
      @CodeCat15  2 роки тому

      I am glad this was helpful, please feel free to ask questions on the topic and do share the channel with your iOS group

  • @dharmendrasinha8806
    @dharmendrasinha8806 2 роки тому +1

    Amazing explanation 🙏

  • @naveenkumardugarbbachhrajd9682

    Amazing work .truly very well explanations as well as the mind of teaching skill you have is just FANTASTIC !!! , would request please become the "Hacking with Swift " of INDIA , and maintain the same quality as well as the content >>> only love !!

  • @PropertiesAI
    @PropertiesAI Рік тому +1

    thanks much for sharing 🙏🏻

    • @CodeCat15
      @CodeCat15  Рік тому

      My pleasure, glad to know this video was helpful 😊 please feel free to ask questions on the topic and do share the video with your iOS group

  • @jayprakashsharma7
    @jayprakashsharma7 2 роки тому +1

    You are doing great job sir. I have never seen any youtuber who is doing such a hard word to create quality videos. Thanks a lot for this channel, please keep it alive.

    • @CodeCat15
      @CodeCat15  2 роки тому +1

      Thank you for your kind words Jay, I'll try my best to deliver more quality driven video. Thank you once again.

    • @jayprakashsharma7
      @jayprakashsharma7 2 роки тому +1

      @@CodeCat15 Ab is se jyda bhi quality video ho sakta hai kya 😀

    • @CodeCat15
      @CodeCat15  2 роки тому +1

      Koshish kar k dekhta hu, shayad thoda aur improve kar pau khud ko, there’s always room for improvement 😊

  • @mushrankhanpathan
    @mushrankhanpathan 2 роки тому +1

    Amazing knowledge sharing!👍

    • @CodeCat15
      @CodeCat15  2 роки тому +1

      Glad it was helpful 😊

    • @mushrankhanpathan
      @mushrankhanpathan 2 роки тому

      @@CodeCat15 yes definitely it is, and keep up the good work with quality, all the very best!👍

  • @umairahmad4368
    @umairahmad4368 2 роки тому +1

    Thank you for explaining the COW concept.
    4:09 Answer (As per my understanding)
    "let" is used for constant only, as we are changing/modifying the "car2" property "name", so we need to use "var" there.

    • @CodeCat15
      @CodeCat15  2 роки тому +1

      but the let did work when we were using a class so why did the compiler time error only arise during struct and not class?

    • @umairahmad4368
      @umairahmad4368 2 роки тому +3

      @@CodeCat15
      Class:-
      with let cause, "class" is considered as "constant of address". It can not be reassigned but can be changed. The property should be with "var" too. Like if we change "var name: String" with "let name: String" then it will not work with the class too.
      Struct:-
      with let cause, it's considered as "constant of value". It is immutable (not modifiable).

    • @CodeCat15
      @CodeCat15  2 роки тому +1

      Thank you Umair for this explanation, and well said 👍

    • @umairahmad4368
      @umairahmad4368 2 роки тому

      @@CodeCat15 Thanks Ravi for counter questions.You always respond to comment section of your video.👍👍💓

  • @jayprakashsharma7
    @jayprakashsharma7 2 роки тому +1

    Class use same memory address that's why they are mutable and value can be modified even we are using let. But in case of struct they share different memory address, it means if you are using let and assigned some value and again you are going to change value it will throw error because you cannot modify value because it share different memory address and it store the previous value also. I think that's why structure are immutable. But in case of class it is mutable because they share same memory address and if we modify the value it means the previous value will be replaced with updated value. It means that the previous value is no longer exist and it behaves like you have not assigned any value earlier. I hope I have explained clearly.

    • @CodeCat15
      @CodeCat15  Рік тому +2

      Thanks Jay for providing a detailed answer, good job.

  • @rahuljamba5846
    @rahuljamba5846 Рік тому +1

    Thats great explanation Ravi, thanks you for sharing your quality knowledge !!

    • @CodeCat15
      @CodeCat15  Рік тому +1

      Glad you liked it! please feel free to ask question on this topic and do share the content with your ios group

    • @rahuljamba5846
      @rahuljamba5846 Рік тому

      @@CodeCat15 sure Ravi I will

  • @abhishekdhiman9771
    @abhishekdhiman9771 2 роки тому +2

    Simple and straightforward!
    Thank you for this video.

    • @CodeCat15
      @CodeCat15  2 роки тому

      Thank you Abhishek 😊 feel free to ask questions on the topic

  • @prataprana2963
    @prataprana2963 2 роки тому +1

    I was waited for this.!!
    Thanks Brother for this easy context❤️

    • @CodeCat15
      @CodeCat15  2 роки тому

      Hope the video answers all your doubts regarding the topic, if not please ask questions in comment or on iOS cafe 😊

  • @pavanmanjani7583
    @pavanmanjani7583 2 роки тому +2

    Hi Nice video
    but I have one query you told reference type doesn't support COW... but in video 9:58 you explained example of COW with class(reference type)

    • @CodeCat15
      @CodeCat15  2 роки тому +1

      Good observation Pavan and thank you for asking this question.
      The memory address that you see on the console are of arr1 and arr2 variables which are arrays and arrays are value types not reference types
      The code change at the mentioned time was to prove that the behavior of COW is not dependent on the elements of the array but the array itself.
      Does that answer your question? If not please feel free to ask more questions.

    • @pavanmanjani7583
      @pavanmanjani7583 2 роки тому +1

      @@CodeCat15 yes got it now
      Thank you for answering
      Keep posting

  • @ranganathchenna4559
    @ranganathchenna4559 2 роки тому +1

    Good explanation
    but the address after change in object is different in both types ... @9:05

    • @CodeCat15
      @CodeCat15  2 роки тому

      And that’s what copy on write is, before change both will point to the same address and after the change the object that you changed will point to a different address

  • @emdutt4012
    @emdutt4012 Рік тому

    Beacuse you are changing the name car in car 2 instance but you take variable let in car2which is constant that’s why it is showing error.

  • @hafizmuhammadbilal4
    @hafizmuhammadbilal4 2 роки тому +1

    @code Cat answer: we are making mutable to change value from structure if have calass then no need to do this because class already have refrence type.

    • @CodeCat15
      @CodeCat15  2 роки тому +1

      Good answer Hafiz 😊

  • @CodeCat15
    @CodeCat15  2 роки тому

    Hey there, I hope this video was helpful. The code shown in the video is uploaded on GitHub, given is the link for the same.
    GitHub link: github.com/codecat15/UA-cam-tutorial/tree/master/CopyOnWrite
    Here's the discord invite link to iOS Cafe: discord.gg/gB3mhP8DNq