Hello, Stanford!

by Louis Tur

As mentioned in my prior post, "Begrudgingly, Swift", I've started going through Standford's iOS8 Swift class on iTunesU. Not only has the course been a welcome challenge, but it has given me a much deeper appreciation of Swift. Previously, I had gone through some random tutorials - which while quite good - didn't really sate my need to know how something works.

I had gone through Stanford's earlier iOS offerings (all taught by Paul Hagerty) for learning Objective-C, so I more or less knew what to expect out of this one. And still, I'm pleasantly surprised with each passing lecture.

Anyhow, I thought I should add my solutions for the 1st assignment of the course in the hopes it might help someone else out. After coming up with my solution, I googled around to see what others had written. And while mine was similar, I wasn't entirely sure if the assignment requirements had changed since other's answers were posted. I believe that my solution covers all requirements, sans one: the autolayout for portrait mode doesn't look great (buttons are squished and text is truncated into ellipses). But I decided not to go back and fix it for this version of the assignment as the next adds some additional layout changes.

I added one bit of flaire (only 19 more to go..) to the UI: the text box will briefly flash on pressing the "clear" button. I thought some visual feedback was needed and it only takes a little snippet of code to do:

// just some UI feedback to let you know something happened
        display.alpha = 0.0
        UIView.animateWithDuration(0.25, animations: {
            self.display.alpha = 1.0
        } )
        UIView.setAnimationCurve(.EaseIn)

While all character entry, including mathematical constants, is handled by appendDigit(_:), I use a small helper method along with an if let statement to handle π.

Everything else is pretty much what could be found already by googling.

For full code and comments, check out the "1st_Assignment" branch of my Stanford repo. Note: You may have to browse prior commits to get all of my code comments.

Louis Tur

"How" has been the single most used word in my literary arsenal for as long as I can remember. I've never really been satisfied knowing that something works, but only by knowing how it works.

Read more from this author