My first blog on Hashnode! I used ChatGPT to create a Todo app in SwiftUI

My first blog on Hashnode! I used ChatGPT to create a Todo app in SwiftUI

ยท

3 min read

What is ChatGPT?

For those who may not be familiar, ChatGPT is an open-source tool that uses natural language processing and machine learning to assist developers in writing code. It can understand and respond to programming-related questions and prompts, and even generate code snippets based on the input it receives.

I recently used ChatGPT to create a Todo app in SwiftUI, and I have to say, it was a really useful tool. While it may not eliminate the need for traditional coding tools and practices, it can definitely be a helpful tool for getting started or for speeding up the development process.

Using ChatGPT to write code

Here was the prompt I gave ChatGPT:

"Write me a simple Todo app written in swift and swiftui"

ChatGPT not only answered within minutes, it provided me with 3 variations of how I could implement this in SwiftUI. Each of the variations were pretty similar, so I will just show you the most impressive one it gave back.

import SwiftUI

struct TodoItem: Identifiable {
    let id = UUID()
    let task: String
    var completed: Bool
}

class TodoList: ObservableObject {
    @Published var items: [TodoItem]

    init() {
        self.items = []
    }

    func addItem(task: String) {
        self.items.append(TodoItem(task: task, completed: false))
    }

    func toggleCompleted(for item: TodoItem) {
        if let index = self.items.firstIndex(where: { $0.id == item.id }) {
            self.items[index].completed.toggle()
        }
    }
}

struct TodoListView: View {
    @ObservedObject var list: TodoList

    var body: some View {
        NavigationView {
            List {
                ForEach(list.items) { item in
                    HStack {
                        Text(item.task)
                            .strikethrough(item.completed)
                        Spacer()
                        Button(action: { self.list.toggleCompleted(for: item) }) {
                            Image(systemName: item.completed ? "checkmark.circle.fill" : "circle")
                        }
                    }
                }
                .onDelete(perform: delete)
            }
            .navigationBarTitle("Todo")
            .navigationBarItems(trailing:
                Button(action: addItem) {
                    Image(systemName: "plus")
                }
            )
        }
    }

    func addItem() {
        // show an alert to get the task name
        let task = "New Task"
        self.list.addItem(task: task)
    }

    func delete(at offsets: IndexSet) {
        self.list.items.remove(atOffsets: offsets)
    }
}

struct ContentView: View {
    var body: some View {
        TodoListView(list: TodoList())
    }
}

ChatGPT then went on to finish with:

"This app has a TodoItem struct that represents a single todo item, with a task description and a completion status. It also has a TodoList class that stores a list of TodoItems and has methods for adding and completing items.

The main view of the app is the TodoListView, which displays a list of todo items using a ForEach loop. Each item in the list has a checkbox that allows the user to toggle the completion status of the item. The view also has a plus button in the navigation bar that allows the user to add a new item to the list.

Finally, the ContentView simply displays an instance of the TodoListView with a new, empty TodoList."

Conclusion

All in all, I was able to paste this code into Xcode, and immediately use the preview editor to test it out! This took me a total of about 15 minutes (just because my mac is slow sometimes ๐Ÿ˜….

One of the drawbacks of using ChatGPT is that it may not always understand your specific use case or requirements, and you may need to provide additional clarification or input. Additionally, the generated code may not always be optimized or follow best practices, so it's important to review and test the code thoroughly before using it in a production environment.

Despite these limitations, ChatGPT is a useful tool that has already proven to be popular among developers. Many people are using it to write code, and it's definitely worth exploring if you're interested in trying out new approaches to coding.

If you enjoyed this blog and want to see more from me, be sure to follow me on Twitter: @theDevInnovator

ย