swiftUI TabBar selection UserDefaults binding not working as expected

Hello,
I have an app with a swiftUI tabBar. It as been working fine for weeks of development.
Recently I wanted to persist the selected tab using UserDefaults. The persisting seems to be happening correctly, however my tabBar will now only display the initially selected tab. Trying to switch to another tab just displays a blank view.

Is there something wrong in this sample code?

Code Block
struct ContentView: View {
    var body: some View {
        TabView(selection: Binding<Int>(
            get: { Defaults.shared.selectedTabIndex },
            set: { Defaults.shared.selectedTabIndex = $0 }
        )) {
            FirstTab()
                .tabItem {
                    Text("First")
            }
            .tag(0)
            SecondTab()
                .tabItem {
                    Text("Second")
            }
            .tag(1)
        }
    }
}
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
struct FirstTab: View {
    var body: some View {
        Text("Tab1")
    }
}
struct SecondTab: View {
    var body: some View {
        Text("Tab2")
    }
}
class Defaults {
    static let shared = Defaults()
    var selectedTabIndex: Int {
        didSet {
            UserDefaults.standard.set(selectedTabIndex, forKey: "selectedTabIndex")
        }
    }
    init() {
        selectedTabIndex = UserDefaults.standard.integer(forKey: "selectedTabIndex")
    }
}

SwiftUI 2 brings a new property wrapper called @AppStorage.

A property wrapper type that reflects a value from UserDefaults and invalidates a view on a change in value in that user default.

You can use this property in ContentView by declaring the selectedTabIndex like this:
Code Block Swift
@AppStorage("selectedTabIndex") var selectedTabIndex: Int = 0

SwiftUI will automatically write the new selectedTab value to UserDefaults and read from it when it is needed.

You can easily pass this value into your TabView like this:
Code Block Swift
TabView(selection: $selectedTabIndex) { ... }


Nice I like that. Thanks.
While I'm waiting for swiftUI 2 to get out of beta, I decided to use this:

Code Block
    @State private var selectedTabIndex = DefaultsManager.shared.selectedTabIndex
    var body: some View {
        TabView(selection: $selectedTabIndex){
            SearchTabView()
                .tabItem{
                    Text("Search")
                }
                .tag(0)
            .onAppear(){
                DefaultsManager.shared.selectedTabIndex = 0
            }
            SecondTabView()
                .tabItem{
                    Text("Second")
                }
                .tag(1)
            .onAppear(){
                DefaultsManager.shared.selectedTabIndex = 1
            }

And tolerate the tech debt of ensuring that tag values match the index values.
swiftUI TabBar selection UserDefaults binding not working as expected
 
 
Q