Geek Culture

A new tech publication by Start it up (https://medium.com/swlh).

Member-only story

From verbose to concise

From Class to Enum: Creating a Haptic Feedback Generator in Swift 5

From heavy to lightweight

6 min readJul 4, 2021

--

A hand touching a large digital display of some sort
Photo by Elia Pellegrini on Unsplash

Say you’re making a singleton or static class to represent data and/or logic that will never change. In many cases, you can convert this logic to an enum and save yourself a bunch of hassle. An enum has much of the functionality of a class, but doesn’t require an instance and doesn’t allow for stored properties — so doesn’t manage state. Instead of properties, you get cases. Instead of values, you get the RawValue. In this tutorial, you’ll refactor an existing static class into an enum.

What Is a Static Class?

A static class is a class that contains only class/static methods and properties. It’s a way of keeping logic contained without creating an instance. The class itself essentially becomes an instance. Much like the singleton (which contains a single instance), it’s often looked down upon because it has the potential to hold global state.

Since you’re only using it in this case for static methods, no state is being held. For more information on global state, singletons, and static classes, there’s a great discussion at SoftwareEngineering.StackExchange.

About the Project

--

--

Kenny Dubroff
Kenny Dubroff

Written by Kenny Dubroff

I’m a Full Stack iOS Developer with a passion for writing clean, reusable code.

No responses yet