avatarZafar Ivaev

Summary

The provided web content explains the difference between the class and static keywords in Swift, focusing on their use in properties and the ability to override the latter in subclasses.

Abstract

The article delves into the nuances of the class and static keywords in Swift, emphasizing that both allow access to properties without instantiating a class. However, static properties cannot be overridden in subclasses, unlike class properties. Through code examples using a Car class and its subclass SuperFastCar, the author demonstrates the compile-time error encountered when attempting to override a static property and resolves it by using the class keyword instead. The article also points out that class-stored properties are not permitted. The discussion is accompanied by visual aids and concludes with an invitation to explore other Swift nuances through additional resources provided by the author.

Opinions

  • The author suggests that using the class keyword is preferable when there is a need to override properties in subclasses, as static properties do not support overriding.
  • The article implies that understanding the distinction between class and static is crucial for Swift developers to write effective and error-free code.
  • The author seems to value the ability to access class properties without instantiation, as it is highlighted as a common feature of both class and static keywords.
  • By providing a "Wrapping Up" section with links to other articles, the author conveys an opinion that continuous learning and exploration of Swift's features are important for developers.
  • The recommendation of an AI service at the end of the article suggests the author's belief in the value of cost-effective tools that enhance developer performance, akin to ChatGPT Plus (GPT-4).

What Is the Difference Between Class and Static in Swift?

With clear examples

Photo by Aditya Joshi on Unsplash.

Today, we will quickly explore the difference between the class and static keywords in Swift.

Before we dive right into code, we usually use the class keyword when we want to access a constant/variable of a class without instantiating that class (just like we use static) and also override that property in subclasses (static properties are not overridable).

The code below is written and run in an Xcode Playground.

Let’s Start

Take a look at the following simple implementation of the Car class:

We can see that we are able to access the countryOfProduction and the topSpeed properties without instantiating the Car object.

What if we want to create the SuperFastCar class inheriting from the Car? We would write the following:

However, this results in a compile-time error — we simply cannot override static properties. What can we do? Use the class keyword instead (note that we cannot use class-stored properties):

Now we see that values are printed as expected:

Wrapping Up

Interested in other nuances of Swift? Feel free to check out my other relevant pieces:

Thanks for reading!

Swift
Programming
iOS
Mobile
Xcode
Recommended from ReadMedium