Summary

In this chapter, we have shown you value types and reference types. The difference between these two types might seem subtle at first. However, you saw that the memory implications make these types fundamentally different. You saw the most important distinction by examining how they behave.

You saw how class instances are passed around by passing the address in memory. This means that mutating one instance mutates everything that points to it. Sometimes, it's not obvious that you're mutating more than once instance. Then, you saw how structs are passed around by value. This meant that all the instances essentially get copied when you pass them around. This ensures that you don't accidentally mutate instances you don't expect to mutate. Finally, you saw how enums are used to contain a finite set of possible cases.

Picking the right option isn't always easy or straightforward; so if you try to stick by the rules of thumb provided, you should be able to make an informed decision. If you turn out to have picked the wrong option, you can always refactor your code to use a different type. It's recommended that you always try to start off with a value type.

In this chapter, it's mentioned that value types don't inherit from other objects. However, they can conform to and implement protocols. This characteristic allows you to use a powerful programming paradigm called protocol-oriented programming. The next chapter will show you exactly what this is and how you can implement it in your own apps.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.168.214