Skip to content

Instantly share code, notes, and snippets.

@scriptingosx
Last active January 22, 2026 06:47
Show Gist options
  • Select an option

  • Save scriptingosx/2cc3b21de662aa02a285878da35f7a03 to your computer and use it in GitHub Desktop.

Select an option

Save scriptingosx/2cc3b21de662aa02a285878da35f7a03 to your computer and use it in GitHub Desktop.
Sample mini SwiftUI ContentView which gets to values from AppStorage/UserDefaults/CFPreferences
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()
}
@scriptingosx
Copy link
Author

scriptingosx commented Jan 22, 2026

Since arguments of the format -keyname value are parsed into CFPreferences/UserDefaults/AppStorage you run the app with ArgumentTest.app/Contents/MacOS/ArgumentTest -greeting Hola -name John and the values will appear in the @AppStorage properties. You can also use UserDefaults.shared.string(forKey: "greeting") to get the value.

The basic CommandLine.argument is also available any where, if you want/need to parse the arguments yourself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment