You Learn Something New Every Day

This is the story of a bored sales engineer enthralled with AI who read Twitter and saw that GPT can write mobile apps. The short version is “sorta,” but I tried to keep the story exciting and educational at the same time. It should be a good read, and if you have questions/comments, go ahead and send them my way.

The setup

So, let’s start with the following:

  1. I don’t know Swift.

  2. I have never written an iOS mobile app.

  3. I am new to AI.

With that in mind, let me tell you a story. It’s a bit long, so hang in there. It’s really neat, and I believe it is the new way of doing things.

The back story

I was on Twitter the other day (yes, I was on the toilet) and came across a post (I will not link it here; you can search) about using ChatGPT to create an iOS mobile app written in Swift. I thought I read that these AIs (ChatGTP, Bard, Bing, etc.) were more on the creative side (writing, art, etc.) and less on the code side. Not to say that they can’t write code, but if you look at Github Co-Pilot, you can see many examples of it writing code for users daily but not designing an app. I was mistaken; it does indeed do that for you, but it all boils down to the prompt and how you write it. As we are discovering, the prompt dictates how “smart” or “dumb” the AI is.

How did you get started?

So here is how I started, first with a solid prompt:

You are an AI programming assistant. 
- Follow the user's requirements carefully & to the letter. 
- First think step-by-step — describe your plan for what to build in pseudocode, written out in great detail
- Then output the code in a single code block
- Minimize any other prose
- Use the latest version of Swift you know how. iOS 15+ is fine. Async/await preferred if you are certain that you can do so. Look out for retain cycles and objects that drop out of memory.
- If a requirement is not technically possible, tell the user.
- If you're making changes to code you've already given the user, don't give the entire file over and over, just give them the changes so they can easily copy and paste and not wait too much

Then, it was time to describe what I wanted to do. Here is my prompt, discussing design guidance, goals, etc.

You're making a full swiftui lifecycle app in one single file that allows gym goers to journal their weight lifting progress over time. The application should be simple and easy to use. The application should also look polished since it will be on the iOS app store. A user should be able to easily track workouts which consist of sets. A set includes a particular exercise (which can be manually added, or chosen from a library of popular strength training exercises), how many reps, and the weight of the reps. The weight selector should be a wheel UI element, sets can be a simple plus-minus. Each set should then be added to the journal when done. Overall weight by exercise needs to be tracked over time. 

This is what ChatGPT started following my instructions and provided the pseudocode.

Step-by-step Pseudocode:

Create SwiftUI views for HomeView, WorkoutView, and JournalView.
HomeView:
a. Display buttons to navigate to WorkoutView and JournalView.
WorkoutView:
a. Display a search bar for user to search exercises or add a new exercise.
b. Display a list of exercises (use a sample library of exercises).
c. On exercise selection, display a new view with details (exercise name, sets, reps, and weight).
d. Implement a wheel picker for weight, stepper for sets and reps.
e. Add a "Save Set" button to save the set and navigate back to the WorkoutView.
JournalView:
a. Display a list of workouts with dates.
b. On workout selection, display a new view with details (exercise, sets, reps, and weight).
Implement Workout and Set models to store and manage workout data.
Utilize UserDefaults to store workout data.

The first version was pretty buggy; XCode wasn’t happy and required a little back and forth, providing lines of code and their corresponding error message. I then asked it to provide the complete code (this is key). Otherwise, it assumes you know what you are doing and takes shortcuts, like “// other code” or something like that. Sneaky, sneaky ChatGPT!

Making progress

It was time for tweaks once it compiled, ran, and functioned reasonably close to the goals. Here is how I handled that; it should give you a sense as to how best to approach it (at least in my experience, which is limited)

A couple of things I noticed working with it: it clearly has a sense of what you are writing and is aware that it is giving you code, but it seems like it doesn’t know what it did 10 minutes ago. Once it tells you something, it forgets what it just told you. I realize it’s not quite like that, but it does *feel* this way.

Top tips

So, there are some lessons learned with GPT, specifically writing code. I tried to distill them down to a solid few, but I have only just begun my AI coding journey, so I am expecting to learn many more:

  • Put your code in CVS (aka GitHub) and save your progress like milestones. For instance, the first version is saved when you get the first version that compiles and runs. The second version is saved when you have made some tweaks and like the way it’s looking/functioning, etc. ChatGPT doesn’t necessarily have your current code, and what it has may not be aligned with your version.

  • Sometimes, you can get into a ping-pong situation with ChatGPT, where it has given you one version with errors, then to fix them, you get a second version, which has errors, so to fix them, it gives you the first version again. Fun times!

  • GPT seems to get “tired”, and stops writing code mid-block, so you will be getting feedback from GPT and it just stops. You can click the retry generation button, and sometimes it’s fixed. You may have to click it several times or ask it to give you output in blocks rather than the entire application.

So what happened?

Ok so, I took my advice (after I got really lost in fixing errors in the code and “ping-ponging” back and forth with GPT) and saved a version that I was happy with (my 2nd version), then used our sessions to explore one feature at a time. I had to “reset” GPT back to my safe version more than once, thankfully I kept it all in local CVS and also on GitHub (you can see my repository here). This is crucial, as I was only given 25 prompts with GPT-4 (which is good), but GPT-3.5 gets you unlimited but is less capable and prone to wrong code (and the ping-pong thing I described above).

As of the time of this post (March 31st, 2023), my app is “ok.”, it runs (you can download it and get it working in XCode, I am sure) and does what I want it to, but there are some serious bugs:

  1. For starters, the first exercise you add to the journal doesn’t list the name, and the # of sets is always 1; weird bug and GPT can’t figure it out (yet)

  2. Putting a graph option in the journal was a day-long slog through the wrong code and “ping pong” sessions until I got frustrated and backed out of my changes.

What’s next?

Well, that's a great question. I suppose it will consist of me finding better AI platforms that are specifically designed for this, trying them out, being disappointed, and coming back to OpenAI. However, I am happy to admit that I am a beginner here, so it’s likely a mix of bad prompt engineering, limitations of the data set that keeps me from being able to click a button, and a major hit on the app store that makes me a multi-millionaire. Perhaps I also have unrealistic expectations, who knows. Stay tuned; I will update this page at some point with the next installment.

Previous
Previous

How can I use AI with my job?

Next
Next

You Learn Something New Every Day (part 2)