Skip to content

Add support for string arrays to AppStorageKey #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ph1ps
Copy link

@ph1ps ph1ps commented Feb 10, 2025

Kind of a weird exception, but UserDefaults supports [String] natively, so I'd suggest to also add support to AppStorageKey. Theoretically, users could create their own workarounds here, by just serializing/deserializing the array and using Data as an intermediate, so this addition might not be necessary.

Alternatively, I could take a look to support arrays in general: https://developer.apple.com/documentation/foundation/userdefaults/1414792-array. However, this might add a lot of overloads.

What do you think?

@stephencelis
Copy link
Member

@ph1ps We've resisted expanding the surface area here to keep things in line with Apple's @AppStorage, which omits this ability, as well as the ability to use Codable.

We're open to revisiting this, but would want to do so in a holistic manner that also considers support for Codable, especially since [String] is technically Codable. We can leave this PR open for now as a reminder, but we might not get back to it right away.

@winnisx7
Copy link

winnisx7 commented Mar 5, 2025

Thanks for the great library!

I've noticed that @shared(.fileStorage) already supports Codable through optional encoder and decoder parameters. However, @shared(.appStorage) doesn't currently support Codable types in a similar way, despite having a very similar API design.

Could you share the reasoning behind why fileStorage supports custom encoder and decoder, but appStorage does not? Would it make sense to align appStorage with the existing fileStorage design to provide consistent support for Codable types?

@stephencelis

@winnisx7
Copy link

winnisx7 commented Mar 5, 2025

Currently, I'm forced into a quite unfortunate workaround:

@Shared(.appStorage("credential", store: .standard))
private var credentialData: Data?

var credential: OAuthCredential? {
  set {
    $credentialData.withLock { credentialData in
      if let newValue = newValue {
        credentialData = try? JSONEncoder().encode(newValue)
      } else {
        credentialData = nil
      }
    }
  }
  get {
    $credentialData.withLock { credentialData in
      if let credentialData = credentialData {
        return try? JSONDecoder().decode(OAuthCredential.self, from: credentialData)
      } else {
        return nil
      }
    }
  }
}

It would be great if @shared(.appStorage) could follow the same pattern as fileStorage, allowing for clean and direct Codable support, thus avoiding such painful boilerplate.

@ph1ps
Copy link
Author

ph1ps commented Mar 5, 2025

I think this should be moved to a discussion, rather than commenting on this MR.

@stephencelis
Copy link
Member

@winnisx7 We are not opposed to exploring what it means to add support for codable, but it certainly isn't straightforward, and the fact that Apple omits support in @AppStorage is telling. This has been talked about in other PRs and discussions, so I'll direct you to a recent one: #48

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants