Learning Swift as a Python Programmer

So I just finished writing my first real iOS application. I’ve written some before, but they really don’t count because I just used PhoneGap. All in all it’s pretty exciting to be able to say I’ve picked up a new skill. This post is particularly barren of too much technical detail, but if anything, perhaps it will inspire the random passerbyers and nomads of the internets that find this page to go and learn a new programming language.

Why I Learned Swift

Reason 1: My wife looked at the web applications I’d excitedly finished after weeks of work and said, “Scott, this is stupid. Why would I use this if it’s not an app on my phone?” So there’s reason 1 for you. And honestly it was known that I’d need to write mobile apps in conjunction with my web apps if the trolls of the internets were to take me seriously.

Reason 2: My resume is starting to look bland. By that, I really mean that I should constantly be growing and stretching myself in some way. As Chase “The Big Redwood Up to No Good” Seibert had suggested to me at one point, it’s a good idea to periodically check your resume about every 6 months and go ahead and update it. In this way it’ll be much easier to remember and articulate your individual contributions to your team. In reviewing my work, there wasn’t anything much in terms of newness to my advertised skillset. The Scott from 2K15 very closely resembled the Scott of 2K14. So I’ve been wanting to get better at something, maybe learn a new language that was specific to a particular domain or perhaps get a scholarly paper published so that I could justify the purchase of a crystal decanter (and then fill it with Everclear, because it’s a delicacy here in San Francisco because it’s illegal). Anyway, writing iPhone apps is perfectly up my alley because I generally enjoy building random things.

The Fastest Way to Learn Swift

As probably everyone would say, time was a huge constraint. And learning something from 0 is also generally frustrating, so it’s nice to just learn as fast as possible. I ended up getting a decent amount of help from my coworkers, Ryan “His Dudeness” Newton and Nag “14K” Chunduru. However, as I found learning Swift and learning XCode are two distinct things.

Learning Swift is actually pretty easy, and coming from another language like Python you can probably pick it up to the level of basic proficiency in under an hour. For me, I just watched a presentation from Andrew Donoho called Swift for Pythonistas. From there, you can generally tread through code with the help of Autocomplete, compilation errors, and quick Google searches until you get the hang of it.

Learning XCode was actually very frustrating for several days until things just sort of start to make sense. In particular, my primary frustration is that most of the resources for how to learn XCode don’t seem to be aimed at developers but are instead aimed at…other people. There are tons of screencasts that show how to accomplish a particular thing with XCode, but the explanation rarely seemed to be given in technical terms that make immediate sense from a programmer’s perspective.

The general practice of building interfaces in my own words is generally:

  • Create some view controllers in the story board and add widgets
  • Control + clicking to a related widget will generally result in creating expected behavior (press a button, end up at another view)
  • All widgets are a default instance of their respective Cocoa Touch Class. For more behavior subclass the default class, associate the widget with the class, then you can control + click from widget to code to gain visibility programmatically

Anyway, my basic strategy for treading through all the information was to go ahead and watch the YouTube screencasts that you can easily find, but I would download them individually with youtube-dl and then watch them at around 4x playback speed. Occassionally there were some nuggets of information that you could extract.

What I like about Swift

After my proclamation that I can safely use Swift without blowing up, there are a ton of things that I really love about it. Some of the basics:

  • You can have explicit immutability; nice for richer abstractions and multi-threading in general
  • Swift is a compiled language, so performance is generally better
  • It’s a statically typed language; easier to understand documentation and 3rd party code, also generally nicer for large applications in my opinion
  • Explicitly nullable or non-nullable variables; prevantable problems resulting from passing around null values really bum me out

As part of its association with Apple’s development environment, you gain pretty easy access to handy libraries in iOS. For example, I thought it would take me a while to make my app play mp4 files, but it actually took less than 5 minutes.

I also found that even from a non-design guy like myself, I could still create an app with a sleek interface that was at home with the iOS ecosystem.

What I don’t like about Swift

In the same way that “Django Unchained” initially tainted search results for Django, searching for answers to problems in Swift results in a large number of pages dedicated to Taylor Swift, who I care nothing about. One time, I went to one of her concerts, and I just felt weird. There was this dude who was a few rows in front of me, and he was really getting into it. There was no headbanging, no strobe lights, no mosh pits, and no weird aromas filling the stadium. So the lesson here is that Apple really should have named the language something like “Methalop”.

If anything, pointing out some of my perceived downers about Swift just makes me realize what I take for granted about Python. For example, sorting a list by some attribute was not nearly as easy as I would have thought, and I quickly missed list comprehensions. As a pretty new language, you’ll also stumble upon incorrect documentation, not so many resources on StackOverflow, and incorrect syntax in online examples as newer versions of Swift caused breaking changes.

Swift was also explicitly created for the domain of creating iOS apps, so it’s inherently tied to Apple’s development environment. XCode also has a number of bugs that you’ll inevitably stumble upon (random crashes, incorrect error messages, errors that require you to resolve by deleting cached files, etc).

As a final nitpick, I’m generally bummed about using XCode as an IDE. I could code much faster by using vim where I’m splitting screens all over the place, yanking, jumping to lines, and generally looking like a badass kind of like Tom Cruise from Minority Report or Jeff Goldblum in the movie Independence Day. With XCode, I’m stuck using the “mouse” where I have to “click” things.

And finally, one thing I noticed about Swift was that there generally seemed to be a lot of ways to accomplish the exact same thing. From Python’s “import this”: “There should be one– and preferably only one –obvious way to do it.” Perhaps that’s more a function of the nature of iOS apps and user interfaces, but it’s just my observation.

My First App

I’ll probably create a follow on post once the app I just submitted hits the streets, but the final thing that was pretty cool about learning Swift was that it instantly validated a premise that I recurringly preach on my blog: make the core of your web application a rich api, and make the user interfaces thinly veiled clients of that API. I was able to write an entire iPhone app that was quite practical as a client to one of my web application without having to modify any server side code.

The End

  • JohnFinch

    I’m surprised no one commented on this. Good blog BTW.

    Appcode is a good IDE for swift development.