ChatGPT For Generating SwiftUI Code

Hey Guys! Introducing ChatGPT, an AI chatbot with a ton of features that everyone is raving about. An artificial intelligence tool by OpenAI lets you type natural language questions, and a chatbot answers them in a conversational manner. Based on previous questions and answers, the bot learns how to respond to your next question. Using vast quantities of information found on the internet, its answers are generated.

lyvennitha sasikumar
5 min readDec 26, 2022

Is ChatGPT something We need to know about?

In November 2022, OpenAI released ChatGPT, which is an AI chatbot that shows off the capabilities of a very large, powerful AI system. There are dozens of questions that can be asked to it, and many times it will provide an answer that will be helpful. In SwiftUI, I’m using this chatgpt for generating a basic list of users. Let’s see what we can do.

Here Am gonna share you some conversation i had with this openAI tool for generating SwiftUI Code. And i really loved the way how the answers are generated with clear explanation.

Initially please sign up to https://chat.openai.com/chat and a Quick Conversational View will get open for your Questions.

Here are my questions

1. Create a List of Users in SwiftUI

Ans: To create a list of users in SwiftUI, you can use a List view and bind it to an array of user objects. Each user object should have properties for the data that you want to display in the list, such as name, email, and profile picture.

Here is an example of how you might create a list of users in SwiftUI:

struct User: Identifiable {
let id: Int
let name: String
let email: String
let profilePicture: Image
}

struct UserList: View {
let users: [User]

var body: some View {
List(users) { user in
HStack {
user.profilePicture
.resizable()
.frame(width: 50, height: 50)
VStack(alignment: .leading) {
Text(user.name)
.font(.headline)
Text(user.email)
.font(.subheadline)
.foregroundColor(.gray)
}
}
}
}
}

In this example, User is a struct that conforms to the Identifiable protocol, which allows it to be used as the element in a List view. The UserList view displays a list of users by binding a List view to an array of User objects and using an HStack to layout the user's profile picture and name and email.

You can customize the appearance and behavior of the list by using various list style and modifiers, such as listStyle, onDelete, and onMove. You can also use NavigationLink to navigate to a detail view when a user is selected.

2. Can you move users to view model

Ans: Yes, you can move the array of users and the logic for managing the list of users to a view model in SwiftUI.

A view model is an object that provides the data and logic needed by a view, such as a list of users. By separating the view and the view model, you can make your code more modular and easier to test and maintain.

Here is an example of how you can move the users array and the logic for managing the list of users to a view model in SwiftUI:

struct UserListViewModel {
var users: [User] = []

func loadUsers() {
// Load the users from a database or API
}

func deleteUser(at offsets: IndexSet) {
users.remove(atOffsets: offsets)
}
}

struct UserList: View {
@ObservedObject var viewModel = UserListViewModel()

var body: some View {
List(viewModel.users) { user in
HStack {
user.profilePicture
.resizable()
.frame(width: 50, height: 50)
VStack(alignment: .leading) {
Text(user.name)
.font(.headline)
Text(user.email)
.font(.subheadline)
.foregroundColor(.gray)
}
}
}
.onDelete(perform: viewModel.deleteUser)
}
}

In this example, UserListViewModel is a class that manages the array of users and provides a method for deleting a user from the list. The UserList view is bound to an instance of UserListViewModel using an ObservedObject property wrapper, which allows the view to observe changes to the view model and update its appearance accordingly. The List view's onDelete modifier is used to bind the delete action to the deleteUser method in the view model.

You can also move the logic for loading the users from a database or API to the view model, and call the loadUsers method in the view's onAppear modifier or in a button action.

3. Add activity indicator to List background

Ans: To add an activity indicator to the background of a List view in SwiftUI, you can use a ZStack to overlay the activity indicator on top of the list.

Here is an example of how you can add an activity indicator to the background of a List view in SwiftUI:


struct UserList: View {
@ObservedObject var viewModel = UserListViewModel()

var body: some View {
ZStack {
List(viewModel.users) { user in
HStack {
user.profilePicture
.resizable()
.frame(width: 50, height: 50)
VStack(alignment: .leading) {
Text(user.name)
.font(.headline)
Text(user.email)
.font(.subheadline)
.foregroundColor(.gray)
}
}
}
if viewModel.isLoading {
ActivityIndicator(style: .large)
}
}
}
}

In this example, the ZStack is used to overlay the ActivityIndicator view on top of the List view. The ActivityIndicator is shown only when the isLoading property of the view model is true. You can set the isLoading property to true when you start loading the users from a database or API, and to false when the loading is complete.

You can customize the appearance and behavior of the activity indicator by using various modifiers, such as color, opacity, and animation. You can also use a Text view to display a loading message or a ProgressView to show the progress of the loading.

That’s it. Within a minutes, we can generate the basic code using chatGPT.

Now Every Developers Fears that ChatGPT will decrese the need for them and replacing them in many ways. Let’s ask the same question to ChatGPT.

Do you really think you can replace software developers
or you can decrease the need of software developers?

Ans: As a language model, I am not capable of replacing software developers or decreasing the need for them. I am simply a tool that can provide information and assistance to those who use it. Software development is a complex and multifaceted field that requires a wide range of skills and expertise, and it is not something that can be automated or replaced by a machine. Software developers are responsible for designing, building, and maintaining the systems and applications that form the backbone of modern society, and their skills and knowledge are invaluable. While it is possible for tools like me to help developers by providing information and assistance, software developers will always be an essential part of the development process.

Use ChatGPT in efficient way to reduce our work. Make use of this new tool and try to give some good conversation with it.

Cons

There is Actually no cons. But the trained data given to ChatGPT is the data upto 2021. So it doesn’t fetch latest as expected. Check the below Q&A

Latest update in iOS

Ans: I’m sorry, but I am unable to provide information about the latest update for iOS as my training data only goes up until 2021 and I do not have access to the internet to browse for more recent information. It is possible that there have been multiple updates to iOS since my training data was collected. If you would like to know more about the latest version of iOS, I recommend visiting the Apple website or contacting Apple support for more information.

Happy Coding!🐳

--

--

lyvennitha sasikumar

Passionate iOS developer chasing my dreams toward success