From the course: Code Clinic: Swift

Solution overview - Swift Tutorial

From the course: Code Clinic: Swift

Start my 1-month free trial

Solution overview

- [Instructor] Here is an overview of my solution to this problem. Keep in mind, this is just how I solved it. You may want to pause the movie here, and try to create your own solution and see how that compares to mine. So, here's my solution. I have some date pickers here, and we can change the days, the months, or the years, and then hit the Update button, and the application updates the data on the screen, and we see that at the bottom of the screen. So, we can calculate the mean temperature between those dates, the median temperature, the mean pressure, median pressure, mean speed, and the median speed of the wind. So, stop the app and just give an overview of my code, how I set that up. The user interface is pretty self-explanatory. Just the user interface elements that you can see are there, there's nothing that is hidden from view currently. For my database, I decided to go with Realm and in my viewController class, I have all of the code. So I import RealmSwift and Foundation and I have a class to manage my data and all of the objects in the class are Realm objects. And I have my Realm created here. In viewDidLoad I load the Realm, so the code in here. Make sure my database is copied to the application's documents folder. I create the configuration file for the Realm and initialize the Realm and then I update the data on the screen in the updateDays method. The updateDays method is the same method that's called when you press the Update button in the application. That shows that activity view which shows the spinner, and then after a short delay. I call the updateDays method. That way we make sure that the activity view is showing on the screen before we try querying the database. So the updateDays method takes the dates inside the date picker, formats them to a format that is appropriate for our database and then we query the database based on the dates selected. If there aren't enough dates, there's an error message shown. If there are enough dates but there's missing data for the start or end date, the user is notified and then we find the averages. The averages take arrays of doubles from the data return from the database query. And then, it calculates the median and mean values and places those within the labels. The median and mean values are calculated through the findMean and findMedian methods. The showMessage method is a utility method for presenting alerts to the user. I also make sure that the start and end dates are appropriate, meaning that the end date is never before the start date. That happens whenever a date is changed. And finally, I make sure the status bar stays hidden. So that's how I solved the problem, and you might want to solve it in your own way. And that's great. Throughout the rest of this chapter, I'll show you how I wrote all the code and my thinking for writing the code as we go throughout each movie.

Contents