Created
October 24, 2024 00:26
-
-
Save takoikatakotako/002c948dd1a04e97b881f9e8f5ffe153 to your computer and use it in GitHub Desktop.
SwiftUIでAppStorageを使ってUserDefaultの値を監視する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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