Last active
January 22, 2026 06:47
-
-
Save scriptingosx/2cc3b21de662aa02a285878da35f7a03 to your computer and use it in GitHub Desktop.
Sample mini SwiftUI ContentView which gets to values from AppStorage/UserDefaults/CFPreferences
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("greeting") private var greeting: String = "" | |
| @AppStorage("name") private var name: String = "" | |
| var body: some View { | |
| VStack { | |
| TextField("Greeting", text: $greeting) | |
| TextField("Name", text: $name) | |
| Divider() | |
| Text(CommandLine.arguments.description) | |
| .font(.footnote) | |
| .frame(maxWidth: 400) | |
| } | |
| .padding() | |
| } | |
| } | |
| #Preview { | |
| ContentView() | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Since arguments of the format
-keyname valueare parsed into CFPreferences/UserDefaults/AppStorage you run the app withArgumentTest.app/Contents/MacOS/ArgumentTest -greeting Hola -name Johnand the values will appear in the@AppStorageproperties. You can also useUserDefaults.shared.string(forKey: "greeting")to get the value.The basic
CommandLine.argumentis also available any where, if you want/need to parse the arguments yourself.