Tuesday, May 12, 2015

On Programming in Swift

I got myself into something called an “iOS Nanodegree” on Udacity.  A course that will teach me all about Swift programming and making apps for iPhones and iPads.  Which is cool.  And something I'd wanted to do for a long, long while.
But they give me homework!
And this is todays homework: Write a blog documenting things learned in the course so far (there’s also a bunch of “how would you do ____ in Swift” topics) but I looked at a couple and, what with a baby and work to do, there’s only so much research I can do in a day.  So: what have I learned in the course so far!
It’s been good fun trying to get the IT part of my brain in gear again.  10 years without writing a function for real (without cheating from code written on the interwebs) makes one really rusty.
So the first thing I learned was how out of touch I really am.  Getting back into the swing of reading code, figuring stuff out, learning a new language, letting go of the half-remembered preconceptions about Java and C# that still float around in my head, have probably been the greatest learning up to now.
I’d sat down maybe a year ago or more to try and figure out Objective-C, and decided it is a stupid, stupid language that should disappear forever.  I realize that this is my love of dot-notation speaking and that I’m probably wrong.  However, not for me.
I then sat down and looked at Mono, because C# is a lot like Java, hey?  But I couldn’t stick with it.  Mono is clunky and boring on a Mac and there aren’t many tutorials for my level of knowledge (full-blown degree in Computer Science, lying dusty in a drawer for the past 10 years).  I know what object oriented programming is and how it works, but I’m ever so out of touch with programming for handheld devices, and I’m stuck way deep in the type of programming that must have a main() class as a starting point.
This course has helped me finally understand the way Xcode (or Cocoa I suppose) manages Outlets and Actions which for me, coming from Netbeans background, seemed the dumbest things in the world.  Why can’t I simply access everything I want to about a button (or any other element) as soon as I stick it on my View (or Canvas as I used to call it)?   Why do I have to do all this Ctrl-dragging around just so I can access methods and properties?  But now, a couple of lessons into this course, it seems to make sense, and at least I now understand how this works.  It makes a lot more sense once you understand the MVC (Model-View-Controller) framework that Apple likes.  The View is what you see on the screen, the Model is the data, the Controller is the code that makes everything work together.  And in this context Actions and Outlets make a lot more sense: Actions are what allow the View (i.e., the User) to send messages (events, or actions) to the Controller.  Outlets allow the Controller (the Code) to modify the properties of things that appear on the screen.  And having to make them explicit means that there’s a heck of a lot better encapsulation than simply having everything open like when you work in Swing (or in Visual Basic or something).
I’ve also learnt all the usual stuff you learn when you’re learning a new programming language: how to declare variables, how to instantiate objects, how to break the compiler because you don’t understand how to pass a variable to a function.  Great fun.

And that's the deal.  Swift is fun.  Some of the names of the objects in the AVFoundation library are way too long, and the API is frustratingly (and at times seemingly on purpose) obscure.  But Swift is fun.  It’s quick and easy to make stuff.  I’m dreading the day I actually have to use a bridging function to Objective-C, and I still need my hand held for a while.  But Swift is fun.  Watch this space for more technical things and lots more geekery as the programming machinery slowly gets churning.