UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Is there a way to programmatically/iteratively assign keys to @AppStorage, @SceneStorage?

Forums > SwiftUI

Let's say I had some code that would allow a user to switch between "pages" and keep track of the last view shown and the number of taps made on each page:

enum ViewRouter: Int {
    case alpha, beta
}

struct Tapper: View { 
    @Binding var count: Int

var body: some View {
        Button("Tap me to ⬆️ value", action: {
            count += 1})
        Text("This page's tap total = \(count)")
        Button("Clear", action: {
            count = 0})
    }
}

struct HomeView: View {
    @AppStorage("screen") var currentView: ViewRouter = ViewRouter.alpha

    var body: some View {
        VStack {
            if currentView == ViewRouter.alpha {
                Page1(viewSwitcher: $currentView)
            } else {
                Page2(viewSwitcher: $currentView)
}}}}

struct Page1: View {
    @Binding var viewSwitcher: ViewRouter
    @AppStorage("tapsForPage1") var tapCount: Int = 0

    var body: some View {
        Text("Page \(viewSwitcher.rawValue + 1)").font(.title)
        Button("➡️ 2", action: {
            viewSwitcher = ViewRouter.beta})
        Tapper(count: $tapCount)
    }
}

struct Page2: View {
    @Binding var viewSwitcher: ViewRouter
    @AppStorage("tapsForPage2") var tapCount: Int = 0

    var body: some View {
        Text("Page \(viewSwitcher.rawValue + 1)").font(.title)
        Button("➡️ 1", action: {
            viewSwitcher = ViewRouter.alpha})
        Tapper(count: $tapCount)
    }
}

Using the keys "TapsForPage1" for Page1 and "TapsForPage2" for Page2 are fine when there are just 2 pages, but what if there were 100 pages, and I'd be setting things up with a single struct named Page with each one having a pageNumber property, e.g.,

struct Page: View {
    @Binding var viewSwitcher: ViewRouter
    @Binding var pageNumber: Int
    @AppStorage("tapsForPageX") var tapCount: Int = 0

    var body: some View {
        Text("Page \(viewSwitcher.rawValue + 1)").font(.title)
        ...
        Tapper(count: $tapCount)
    }
}

Is there a way to say "I want the key for each Page's @AppStorage (or @SceneStorage?) wrapper to be "tapsForPage\(pageNumber)"?" Is this even the correct method for going about doing what I'm attempting?

1      

From my understanding you can't change the name of a @AppStorage (aka UserDefault key) at runtime.

1      

How I Got My Scammed Cryptocurrency Back

Do not, under any circumstances, entrust your cryptocurrency to these online investing firms. When I requested a withdrawal during the epidemic, they wanted me to make a larger investment despite the fact that I had deposited over $100,000 with them. Why, for instance, would I add additional funds while they blocked me from withdrawing any? When I told them I needed my money returned, they kept making excuses, but eventually stopped returning my calls or answering my emails. I told a colleague about everything, and he suggested WIZARD LARRY HACKER, so I messaged him. I must admit, I was impressed by the speed of his response. I received my money back because of how expertly they handled my case. WIZARD LARRY comes highly recommended. wizardlarry@mail.com WhatsApp +1 (205) 319-6886

1      

@AppStorage can save data.

Can you create a dictionary (Int: Int) where the first int is the page number and the second int is the number of taps. Then convert that dictionary to data to save in AppStorage? With a property observer on your dictionary, it should be able to save every time the page changes or the number of taps changes and save that to the @AppStore after a conversion.

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.