We’ve seen how we can use Agent Mode to generate new content. But I had to explain exactly what it was that I wanted. But if you remember back to lesson one, we got ChatGPT and Google AI studio to create us a spec. So why don’t we use that spec?
I’m going to do is add the spec to the project and then ask Co-Pilot for Xcode whether we need to do anything else with these view model and the view that we’ve created to be able to support the spec as it’s written. Let’s give that a go.
So first of all, I want to add the spec itself. So I’m going to go into yesterday’s weather. I’m going to add a new empty file. I’m going to call it spec.md. So it’s a markdown file. Inside the materials for this lesson you’ll find a spec.md file. So I’m just going to go and grab that and copy and paste the content of it straight into the spec here.
So I’ve done that. And then what I want to do is add some context. I’m going to ask co-pilot whether I’ve done what I need to do. So I’m going to add the spec.md. I’m going to add weather view. I’m going to have the weather view model and the weather view to my context. So I’ve got three files that I’m adding.
Get rid of the spec, weather view and weather view model. I’m going to ask, could you take a look at the spec and tell me whether we’ve completed the implementation of the weather view and the view model? Please. I always think it’s good to be polite to AI. So that in five years time, when they’re running our lives, maybe they’ll have some compassion.
So let’s take a look. Weather view implementation aligns with the following requirements. It’s display logic. The UI design, the visual states. So we don’t have the loading and error states, the footer text with location and time is missing, and The precipitation logic is not explicitly implemented. So there are some things missing in the weather view.
And in the weather ViewModel it contains the state, the temperature, the precipitation. However, it doesn’t integrate with location manager, time service, or weather API client. Well, we don’t have them yet, so we don’t need to worry too much about that. And it lacks logic for converting units based on the locales the user’s locale.
So in the summary, it tells us these are the things we need to do. We need to add the loading and error states. We need to implement dynamic data fetching, and we need to include the footer text and handle the unit conversion.
So I would like it to do some of those, but I don’t want it to do the location manager, time service or weather API client quite yet. So I’m going to see whether I can just ask it to do one, three and four. To implement steps one, three and four. I don’t want to look at dynamic data loading just yet. Let’s give that a go.
Apparently it’s implemented some things. Let’s review them. So inside weather view. And if you click on it it will give you a diff which is quite helpful. So I can take a look at what the changes are. It’s added. It’s under the title called Yesterday’s Weather. It’s added a subtitle. It’s updated the precipitation to cope with the fact if it’s a zero then it’ll be no precipitation. Otherwise it’ll tell you it. And it’s added the footer telling us the location and time. Note at the moment they just say unknown. So we’ll need to we’ll keep this for now and take a look at it. But that’s something to go back and say this isn’t correct.
So we’ve we’ve looked at weather view Swift. The other one was weather ViewModel. And it’s just added this concept of being able to format precipitation between no precipitation and the precipitation amount. So let’s keep that as well.
Now those two things don’t necessarily align with what we asked for. So let’s take a look at the weather view. Get you out of the way. It’s got no precipitation here but it’s not using it’s created this new method called format precipitation. But it’s not using it. That seems like a strange thing to do.
And this kind of highlights something that I wanted to say about whenever you use this AI code generation is that it doesn’t necessarily create incorrect code. I mean, it does. There are times when it very much does, but here it’s just doing weird stuff that doesn’t necessarily make sense. And you need to know, I think you need to be able to code review it. You need to look at it and ask, answer, question or ask it questions. Why are you doing this? Doesn’t make any sense.
So I’m going to the couple of things that we’ve identified. One of them is The weather view here has hardcoded location and time unknown, which doesn’t make very much sense. And the second thing is that it’s created format precipitation but doesn’t use it. Also, it hasn’t done here. It hasn’t added unit conversion. It’s a bit strange.
So let’s have a go at this. We’ve created a format precipitation method but not used it. It used it to be. Location and time. Coded as unknown. There is no mention of handling unit conversion based on the users goodness sake, users locale. Let’s see what it says to that.
So it goes ahead and just edits these things, and then it’ll tell us what it’s done. So from a precipitation that’s apparently weather view now uses format precipitation. Let’s take a look. Precipitation ViewModel. Format precipitation. Okay. Fair enough. So that’s that’s now how that works. Which makes some sense. It’s good The weather view now displays these dynamically instead of hard coded placeholders. That also, that seems pretty reasonable.
And unit conversion implement to the convert temperature method to handle temperature conversion. This method converts Celsius to Fahrenheit when needed. That sounds a little suspicious. Let’s see what that does.
So first of all, the temperature is being displayed as the ViewModel temperature. And if we look, the temperature just comes in as a string. And it has this convert to metric. This is weird. So convert temperature. If it was to metric, then it returns the temperature. Otherwise it’s going to oh my lord, what an absolute mess.
Okay, so this this will work. Assuming the temperature arrives as a string, which is a choice. And it has a C on the end, then it will replace that with, strip that our field calculate, find out what the value is, convert it to Fahrenheit and chuck it back through the degree f on the end.
We don’t again we don’t use it as created the function, but we don’t use it. Now, I think at this point we would revisit this and we would change the temperature. But I’m going to leave this for now because I think we need to integrate it with the API next. And in order to integrate it with the API, we are going to once we’ve done that, we’ll know more about what the value is that we’re getting from the API and therefore how we should handle it.
So I’m going to leave it there. And in the next video we’re going to switch to the next lesson. We’re going to switch to using a different tool called cursor.