Final Class in Swift: When and Why to Use it

Jerry PM
3 min readMar 6, 2023

--

Source: Author

As an iOS developer, you may be familiar with the concept of a final class. A final class is a class that cannot be inherited, meaning it cannot be subclassed or extended. It is similar to a static class. To declare a final class in Swift, you simply add the ‘final’ keyword before the class name. For example:

A final class is a class that cannot be inherited

Final classes are ones that cannot be inherited from another class, which means it’s not possible for users of your code to add functionality or change what they have it's like a static class, If you make it final, it won’t compile, as it has subclasses. to make a class final just put the final keyword before it

final class TestFinalViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}

There are several reasons why you might want to use a final class in your Swift development. One of the main benefits is that it provides a level of protection for your code. When a class is final, users of your code cannot add functionality or change what they have. This can be useful in situations where you want to ensure that your code remains consistent and stable.

Another benefit of using a final class is that it can improve performance. Because a final class cannot be inherited, the compiler can optimize the class more effectively. This can lead to faster execution and better memory management.

However, it’s important to note that using the ‘init’ method in a final class can cause errors, leading to a BAD_ACCESS error when running the app. To avoid this, you can use a ‘static let’ instead. For example:

When deciding whether or not to use a final class, it’s important to consider the intended use and functionality of the class. If you want to prevent users from extending or modifying the class, using a final class may be appropriate. However, if you want to allow for flexibility and extensibility, it may be better to avoid using a final class.” To fix this issue just use “static let” .

final class TestFinalViewController: UIViewController {
static let shared = TestFinalViewController()
override func viewDidLoad() {
super.viewDidLoad()
}
}

While there are benefits to using a final class, it’s important to consider the intended use and functionality of the class. If you want to prevent users from extending or modifying the class, using a final class may be appropriate. However, if you want to allow for flexibility and extensibility, it may be better to avoid using a final class.

Ultimately, the decision to use a final class in your Swift development will depend on the specific needs of your project. It’s important to weigh the pros and cons and consider the potential impact on the stability, performance, and extensibility of your code.

Conclusion

I hope this article has been of help to you. practice, and improve your skills. Make you better developers.

Become a better developer with my templates. Your support by purchasing one of my templates would be greatly appreciated. Thank you.

My Swift UI Wallpaper app Filter and you can edit your image too from your photo in this template has been sold on the CodeCanyon website. Thank you for reading my article.
https://codecanyon.net/item/wallpaper-wizard/43965325 😄

--

--