Member-only story
ChatGPT Does SwiftUI
Not that I’m worried, but…

ChatGPT is a demonstration project by OpenAI that can converse on a number of topics. Including programming.
Let’s take it through its paces, shall we?
ChatGPT Conversation
Everything that follows from this point forward is my input prompts, followed by ChatCGP’s responses, complete with sample code.
Hey ChatGPT, can you make a SwiftUI entry form with name, address, city, state and zip fields?
Sure! Here’s a simple example of a form in SwiftUI that has fields for a person’s name, address, city, state, and zip code:
import SwiftUI
struct EntryForm: View {
@State private var name: String = ""
@State private var address: String = ""
@State private var city: String = ""
@State private var state: String = ""
@State private var zipCode: String = ""
var body: some View {
Form {
TextField("Name", text: $name)
TextField("Address", text: $address)
TextField("City", text: $city)
TextField("State", text: $state)
TextField("Zip code", text: $zipCode)
}
}
}
In this example, the EntryForm
struct conforms to the View
protocol and defines a body
property that returns a Form
view. The Form
view contains TextField
views for each of the form fields, and the @State
property wrapper is used to manage the state of each field.
Can you move the fields to a view model?
Yes, you can move the form fields to a separate view model in order to better manage the state and logic of the form. Here’s an example of how that might look:
import SwiftUI
struct EntryForm: View {
@ObservedObject var viewModel: EntryFormViewModel
var body: some View {
Form {
TextField("Name", text: $viewModel.name)
TextField("Address", text: $viewModel.address)
TextField("City", text: $viewModel.city)
TextField("State", text: $viewModel.state)
TextField("Zip code", text: $viewModel.zipCode)
}
}
}
class EntryFormViewModel: ObservableObject {
@Published var name: String = ""
@Published var address: String = ""
@Published var city…