pointfreeco / swift-composable-architecture

A library for building applications in a consistent and understandable way, with composition, testing, and ergonomics in mind.
https://www.pointfree.co/collections/composable-architecture
MIT License
12.22k stars 1.42k forks source link

Add array support for Shared appStorage #3061

Closed dadalar closed 4 months ago

dadalar commented 4 months ago

This PR adds support for the following Shared states:

// First use case: Store an array of strings
@Shared(.appStorage("stringArray")) var texts: [String]

// Second use case: Store an array of RawRepresentables
enum Direction: String {
  case north, south, east, west
}
@Shared(.appStorage("directions")) var directions: [Direction] = [.north]
stephencelis commented 4 months ago

Hi @dadalar! Thanks for taking the time to PR. For now we want to keep appStorage more or less limited to the APIs Apple provides for @AppStorage in SwiftUI. Apple seems to want to encourage sending only simple data to app storage, and they decided to omit the support for arrays, even though UserDefaults does have some array support. If this changes in the future we for sure would revisit.

For now if you need support for this we recommend defining it directly in your project.

We closed a similar PR extending appStorage's APIs here: https://github.com/pointfreeco/swift-composable-architecture/pull/2905

I'm going to close this for now, as well, but feel free to open a discussion if you have more you'd like to chat about!