Skip to content

Instantly share code, notes, and snippets.

@takoikatakotako
Created October 24, 2024 00:26
Show Gist options
  • Select an option

  • Save takoikatakotako/002c948dd1a04e97b881f9e8f5ffe153 to your computer and use it in GitHub Desktop.

Select an option

Save takoikatakotako/002c948dd1a04e97b881f9e8f5ffe153 to your computer and use it in GitHub Desktop.
SwiftUIでAppStorageを使ってUserDefaultの値を監視する
import SwiftUI
struct ContentView: View {
@AppStorage("FAVORITE_POKEMON_NAME") var favoritePokemonName: String = ""
var body: some View {
VStack(spacing: 16) {
Text("Your favorite pokemon is, \(favoritePokemonName)")
Button("Snorlax is my mavorite pokemon.") {
favoritePokemonName = "Snorlax"
}
Button("Slowpoke is my mavorite pokemon.") {
UserDefaults.standard.set("Slowpoke", forKey: "FAVORITE_POKEMON_NAME")
}
}
}
}
#Preview {
ContentView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment