Classes in Swift...Why Bother?

Hi All,

After reading the Swift apprentice, I must say that I that I am having a very hard time to ever see the need for the use of classes in Swift programming. As a newbie to Swift, I can completely understand the point of classes being a very small part of the standard library as described in Chapter 25.

However, there must be a good justification to keep classes as part of the language. I am just struggling to see practical uses for classes. The use of protocols and structs seems like the best way to go in a way similar to the retail store challenge. Can anyone change my mind? What would be a good practical use case for them? When should you consider implementing them?

Thanks for your insights in advance. Looking forward to read other points of view.


I like to illustrate the value of classes by writing code to work with geometric shapes. Triangles, squares, pentagons, etc. all have a perimeter and area. The area of each shape has it’s own formula. So, there’s a super class called SHAPE which might have properties or methods that are common to every share – outline color, fill color, fill pattern. Then there is a TRIANGLE class, and SQUARE class, etc. which all inherit from the SHAPE superclass but have unique AREA methods.

Now I can have an array of SHAPEs. The zeroth might be a triangle, the 1th might be a RECTANGLE, etc. To calculate the total area of all the SHAPEs in the array, I can iterate thru the array and call the AREA method for whatever might be at that index. I don’t need to know what the object is or what area formula to use. The class knows it’s own formula.

Hi There,

Thanks for the reply. I understand what you are staying and I can see the power of objects especially in an object based language. However, this looks like something that could be handled with Protocols just as easily. If I learned correctly, you could create a Shape protocol with an area property. From here, you could then have a struct (or even a class) for each shape which adopts that Shape protocol. In each struc you can define the appropriate formula for the area. In my mind, you effectively achieve the same thing.

It seems to me that you should pretty much always use the protocol methodology. I guess I am still confused as to when you should truly apply the object methodology over the protocol methodology.


Please read my tutorial about object oriented programming concepts, pattens and techniques when you actually get a chance: in order to understand everything even better. Let me know if you have any questions or issues regarding the whole thing: it actually uses a band of instruments (piano, guitar, acoustic guitar, electric guitar) as its main theme.

@jbreen. I realize it has been a while since this question was answered, but I came across a similar question, and the solutions that were provided for it was something I felt I had to share with you. Your question is quite relevant, and can benefit many others as well.

I hope this helps!

All the best!

@shogunkaramazov. My apologies, I am not trying to step on your toes in any way!