Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
The Ultimate iOS Interview Playbook

You're reading from   The Ultimate iOS Interview Playbook Conquer Swift, frameworks, design patterns, and app architecture for your dream job

Arrow left icon
Product type Paperback
Published in Aug 2023
Publisher Packt
ISBN-13 9781803246314
Length 320 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Avi Tsadok Avi Tsadok
Author Profile Icon Avi Tsadok
Avi Tsadok
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Preface 1. Part 1: Everything about Interviews
2. Chapter 1: Before the Interview FREE CHAPTER 3. Chapter 2: Going through the Interview Process 4. Chapter 3: Developer Branding 5. Part 2: Swift Language and Coding
6. Chapter 4: Data Structures and Algorithms 7. Chapter 5: The Swift Programming Language 8. Chapter 6: Managing Your Code 9. Part 3: The Frameworks
10. Chapter 7: Building Great User Experiences with UIKit 11. Chapter 8: SwiftUI and Declarative Programming 12. Chapter 9: Understanding Persistent Memory 13. Chapter 10: Libraries Management 14. Part 4: Design and Architecture
15. Chapter 11: Design Patterns to Solve Complex Questions 16. Chapter 12: Drilling into App Architecture 17. Chapter 13: Acing the Coding Assessment 18. Index 19. Other Books You May Enjoy

Sharing a state using Singleton

There are two questions that interviewers love to ask:

  • “How do we create a Singleton?”
  • “Is it good to have singletons in our app?”

The first question is technical, but the second one is tricky.

Let’s start with the definition of a Singleton.

What is a Singleton?

In the Singleton design pattern, there is only one instance of a class that can be globally accessed through a static property. It is often used to manage shared resources or states in a program where multiple instances could cause issues with synchronization or consistency. To implement a Singleton, a class typically has a private constructor and a static method or property that returns the single instance of the class.

In Swift, it is simple to create a Singleton. We use a static property for that task:

final class MySingleton {    static let shared = MySingleton()
    private init(...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image