Earlier this week I lamented the fact that life often times gets in the way distracting us away from the goals we set for ourselves. At that time I noted that due to time and family commitments I found myself nearly a week behind in the 100 Days of Swift challenge. Rather than give up, I dusted off my keyboard and got back into the programming chair.
The road back helped because I happened to be at the Consolidation Days which were reviewing what I learned during the first 12 days and acted as a natural starting point before getting into new ideas and concepts. Those three days were extremely helpful as I outlined here, here, and here. The calendar also provided a big assist as I got to the projects just as the weekend started. And not just any weekend, but one that saw Arizona get rain or snow depending on where you were located meaning it would be a full weekend spent indoors. From a scheduling perspective, the planets had all aligned.
At this point I should probably give a little bit of self-disclosure. I have a severe personality defect where once I get engaged with something, I tend to become slightly obsessed. There is some debate by my family as to what exactly constitutes “slightly”. So when the Swift challenge moved to actually building applications I became the equivalent of a human sponge continuously gathering information and going what seemed like a million miles an hour.
Sleep became something that could not or would not come freely. Instead all I could think about was code and projects going over in my head the various aspects first of building a Storm Viewer app that simply allowed the user to select from a list of 10 pictures to look at storm clouds. That sounds simplistic enough but it was exciting with each declaration or instantiation of a UI TableView Controller or writing a method to accomplish some kind of functionality. I’ve never been addicted to drugs but this must be what it feels like to not be able to get enough as you go through each screen and write each line of code.
Before long I had completed all three lessons for Project 1 and additionally not only completed the three challenges but then created several more finding APIs where you could pull weather data and wondering if it would be possible to pull images in real time from sources that showed storms in action.
After that exhilaration of completing the first project I eagerly looked forward to the second. This was developing a game. I will admit, gaming has never been a big deal for me, and I am not even sure I have a game loaded on my iPhone. But that did not matter, this was code and the opportunity to build yet another app.
This assignment was to create a game that displayed three flags from countries around the world and allow the user to guess which flag went to a specific country. It was extremely helpful as it built directly upon the last project that we did. Before long, my fingers were blazing across the keyboard attempting to follow along with the lectures but then asking myself “what if” and going down a path to try additional scenarios.
The project allowed us to delve into asset catalogs and understand how they work and how Apple has provided an easy way to deal with device resolution and pixel density without the developer having to worry about it. We finally were able to see a closure in the wild as we worked through Alert Controllers and actions (in the immortal words of the talking M&M’s, “they do exist!”).
The challenges were especially fun with this project although I have to admit, I am not sure my solutions are pretty or even if they are legit. For example, the first challenge was to show the player’s score in the navigation bar alongside the flag to guess.
At first I thought it would be easy, just add a label to the Navigation Bar on the left side in the UIViewController using the Storyboard. You would think that logically that would be the thing to do. Despite multiple attempts, I could not get a label to display on the Navigation Bar except for the Title which was already being used. I guess I could have appended the score with some space but that seemed like a hack. So I took a more creative approach.
I created a Navigation Item Button and placed it on the left of the Navigation Bar. I changed the color of the new button to be the color of the title. I then made the button text be Score:. After creating an outlet for the button in the UIViewController I would change the value of the text using:
totalScore.title = “Score: \(score)”
The button was named totalScore and I used string interpolation to add the current score value to the string and repopulate it. It works but I have no idea whether that was how it should have been done. I am sure other programmer will look at that and scratch their heads but hey you go with what you know.
The second challenge was to show an additional alert message when a player reached 10 questions. That was relatively simple to track with a counter for number of questions asked. I took it a step further to tell the user what question they were on out of 10 so there wasn’t any confusion. Once the user answered 10 questions they got a different message in the alert then the counters and score were reset to start again. I also added a button on the right of the navigation bar called “New Game” that allows the user to start over whenever they want.
The third and final challenge was to provide some education to the user when they selected the wrong flag. Instead of just “Wrong” the message would tell the user what country’s flag they did select in error. This was another time where I am not sure whether my solution is correct but it does work. I did a simple string interpolation on the title text but I may not have used the right thing to get the answer. I went with:
title = “Wrong, that’s the flag of \(countries[sender.tag].uppercased()).”
Since the application points all three buttons to the same buttonTapped function in the UIViewController, I needed to figure out which button was pressed. In the Storyboard we set an ID for each of the three buttons (0,1,2). I just needed to figure out which button was actually selected. After trying buttonTapped and getting a bunch of errors I realized that it was getting values via sender. After a couple of searches and reading some documentation I realized you could get tag from there so sender.tag was the actual button that was pushed. Since that equates to one of three items from the countries array, I figured I had it. It worked. Not sure it’s what they had in mind, but it worked.
Overall it has been a lot of fun over the first two projects. Given my excitement, I was able to compress six days into two so now I am only three days behind schedule. As I mentioned before, getting caught up is not the point. The goal is to devote 100 days to learning Swift. Sometimes it might take more than a day to complete something and that’s ok as long as we keep working on it.
0 Comments
Trackbacks/Pingbacks