My Initial Question
ChatGPT came to mind. I explained why I needed this variable, and with some context, I hoped it would suggest a good name. Here's my original question:
I have a main view with some tab bars, such as Edit, Tap, and Report. The main view is the starting point. I would like to define an enum type to indicate where the starting point is, for example, TabBarStatus. I also like to define a variable for the status. What names should I use for the enum and variable? The variable will be used in subsequent views to find out where the start view is.
Less than a second later, ChatGPT responded with a thoughtful explanation and code examples.
For your use case, where you want to track which tab was the starting point (Edit, Tap, Report, etc.), defining an enum and a variable to hold that state is a great approach. Here's a naming suggestion that is clear, consistent with Swift/iOS conventions, and scalable:
Use a descriptive enum name that reflects the context:
- enum MainTab {
- case edit
- case tap
- case report
- }
Alternative names:
- StartTab
- MainTabType
- TabBarStatus (your suggestion, but slightly less Swift-like)
Swift style prefers concise, noun-based enum names like MainTab or StartTab.
For a variable that stores the starting tab, here are a few clean suggestions:
- startingTab: MainTab
- originTab: MainTab
- entryTab: MainTab
- launchTab: MainTab
- startFromTab: MainTab (if used in routing context)
- enum MainTab {
- case edit
- case tap
- case report
- }
- struct ViewModel {
- var startingTab: MainTab
- }
My Follow-Up
Just like talking to a senior developer, I continued the conversation and explained more about the context and how this variable would be used:
Good. Another question I need your advice on. All main view controllers and subsequent view controllers (VCs) are based on a base view controller,BaseViewController: UIViewController
. All the subsequent VCs are presented as detail views, modal views, etc. I would like to define a public variable inBaseViewController
, and its value is set in the main view controllers. I think this var should be static, instead of an instance variable. The reason is that there is only one starting tab. Right?
I was hoping ChatGPT could offer a better design based on my explanation. To my surprise, ChatGPT not only understood my design but also laid out a solid analysis of how the design could be improved.
Great question—and you're thinking in the right direction! Let's break it down and figure out the best approach.
You want all view controllers (which inherit from BaseViewController) to know where the user started (i.e., which main tab). There's only one starting tab per app session, and this info is set by the main view controller.
- Static variable (static var or class var) is shared across all instances of BaseViewController. Perfect for global, app-wide state.
- Instance variable (var) is unique to each instance, so you'd need to pass it manually to every view controller—probably not what you want here.
👉 Since there's only one starting tab, and you want all view controllers to access the same value, a static variable is the right choice.
- class BaseViewController: UIViewController {
- // Default value, set from main controller
- static var startingTab: MainTab = .tap
- }
Then, in your main tab view controllers (e.g. EditViewController, TapViewController, etc.), you set it like this:
- override func viewDidLoad() {
- super.viewDidLoad()
- BaseViewController.startingTab = .edit // or .tap or .report
- }
And any other view controller can read it like:
- let origin = BaseViewController.startingTab
- class BaseViewController: UIViewController {
- private static var _startingTab: MainTab = .tap
- public class var startingTab: MainTab {
- get { return _startingTab }
- set { _startingTab = newValue }
- }
- }
Let me know if you also want to persist this value (e.g. between launches) or just keep it in-memory for one app session.
A Better Design
During this very natural and productive conversation, three concepts caught my attention: state, persistence, and computed property. They suddenly lit up my thinking like a flash of inspiration. A new design came to mind, and I shared it with ChatGPT:
There may be some cases where a subsequent view is not inherited fromBaseViewController
. I think it may be better to define a static class, such asMainTabStatus
(or maybe a better name?). Within the class, a privatevar _startingTab
and a public computed propertystartingTab
can be used to manage the starting tab status. This class can be extended to include mechanisms to store the status in memory or user settings (persisting across app launches). This helper class can then be accessed from any view controller, view, or class to retrieve the starting point status.
Very quickly, I got feeback from ChatGPT.
Yes, that’s a great idea—and a cleaner and more modular design! Creating a dedicated MainTabStatus (or similarly named) helper class decouples the logic from your view controllers entirely, making it more flexible, easier to test, and extendable for future needs (like persistence).
- enum MainTab {
- case edit
- case tap
- case report
- }
- final class MainTabStatus {
- private static var _startingTab: MainTab = .tap // default value
- public static var startingTab: MainTab {
- get { return _startingTab }
- set { _startingTab = newValue }
- }
- // Optional: Save to UserDefaults if needed
- public static func persist() {
- UserDefaults.standard.set(_startingTab.rawValue, forKey: "StartingTab")
- }
- public static func restore() {
- if let raw = UserDefaults.standard.string(forKey: "StartingTab"),
- let tab = MainTab(rawValue: raw) {
- _startingTab = tab
- }
- }
- }
🔧 For the UserDefaults part to work, you'd make MainTab conform to RawRepresentable (usually with String), like this:
- enum MainTab : String {
- case edit
- case tap
- case report
- }
Set at startup:
- MainTabStatus.startingTab = .edit
Read anywhere:
- let origin = MainTabStatus.startingTab
Restore from UserDefaults (if persisted):
- MainTabStatus.restore()
If you want something shorter or more Swift-y:
- AppNavigationContext
- StartContext
- OriginTab
- TabTracker
- MainTabTracker
MainTabStatus
is already clear, so it's a solid choice.
Let me know if you'd like this helper to also handle notifications (e.g., broadcast when tab changes), or if you want a sample using @AppStorage
(SwiftUI), too!
Wow! What a great set of suggestions from ChatGPT—starting from a simple question about naming a variable, and evolving into a much better and scalable design.
Other Blogs
- Next: Common Issue for Updating iOS App
- Prev: Huawei P40
0 comments:
Post a Comment