Swift and Learning New Languages

by Rob Poulter


Like a whole bunch of people I was quite interested to learn about Apple's introduction of their new language Swift, at their WWDC event this year. I've been trying to come to terms with ObjectiveC for a couple of years now without much success and this seemed like a good opportunity to learn something new and at the same time finally get around to starting to write some iOS and potentially OSX software.

It's been quite some time since I went and learned a new language. I had a brief look at Mozilla's Rust a while back, and while some of the language features were interesting, I didn't have a compelling reason to really get into it and try to write something. I think that means the last time I actually learned and used a new language was Python, going on 10 years ago (and I still thoroughly enjoy using it).

Learning Swift has been relatively easy for most of the basic language features as it shares a lot of the nice higher level syntax of commonly used interpreted languages like type inference and less focus on throwing special symbols around (like semi-colons in any C-like language, and in particular square brackets in ObjectiveC). Some of the funkier things have taken a bit more getting used to, like Optionals and Generics, but since the WWDC session videos were available to everyone this year, it was quite helpful seeing their presenters go through a bunch of examples explaining their uses.

I was having a play with creating custom operators this afternoon, since while Swift has an 'in' keyword like Python, it doesn't operate the same way. You can use it to iterate over an iterable, but not to check for membership, which is often handy. I figured I'd have a go at making an operator that did and it seems to work ok (but no doubt will break in all sorts of interesting ways!)

Update 2014-08-20: From Beta 4 a few things changed, so I've aded a few things to fix this so it still works.

// This bit on extending string via subscripts from
// http://stackoverflow.com/a/24144365/2930510
extension String {
subscript (i: Int) -> String {
return String(Array(self)[i])
}

subscript (r: Range<Int>) -> String {
var start = advance(startIndex, r.startIndex)
var end = advance(startIndex, r.endIndex)
return substringWithRange(Range(start: start, end: end))
}
}
infix operator<> { associativity none }

func <><T: Equatable>(left: T, right: [T]) -> Bool {
if let result = find(right, left) {
return true
} else {
return false
}
}

func <><T: Equatable>(left: T, right: [T: AnyObject]) -> Bool {
if let result = right.indexForKey(left) {
return true
} else {
 return false
}
}

func <>(left: String, right: String) -> Bool {
let strlen = Array(right).count
let sublen = Array(left).count
    for i: Int in 0..<(strlen-sublen+1) {
        if left == right[i..<i+sublen] {
            return true
        }
    }
for i: Int in 0..<strlen {
if right.substringFromIndex(i).hasPrefix(left) {
return true
}
}
return false
}
 (This would be nicer with properly indented code :-/)

It's a little annoying that I couldn't just use the word 'in' with this approach, as operators have to be made up of the usual operator set of characters (!, -, + etc) and not much really shouted out membership to me. Messing with anyone who uses the <> convention for inequality and reads my code makes me a little bit happier though :) I quite like the use of generics here for the array and dictionary operations, even if the method for finding a substring just makes me cringe.

The real headache (as lots of people pointed out when Swift was announced) is not that Swift is a nice easy language to learn, but that all of the iOS and OSX frameworks still need to be learned, and they're less straightforward. The majority of the tutorial code that people have put out there is still in ObjectiveC, and while it is possible to translate from one to the other after a while, when I was trying to figure it out to start with it was still quite obtuse. Add to that the XCode beta still being quite flaky in some respects when it comes to Swift and having to fall back on ObjC code for some things (like unwinding segues between storyboards) and getting a very simple app written turned out to be not quite as simple as all that.

One of the things that has quite surprised me is that so few tutorials aimed at iOS beginners actually do much to explain the concepts of how the application model works. Most tutorials focus on explaining what code does, which is fine, but if you're not aware of what a view controller does, or the purpose and application of delegates then you're left pretty confused.

In summary, it's been quite fun playing with a new language and doing something practical with it. Now that I've read a small mountain of articles on basic app design I think I'm starting to get my head around the way that iOS works and can start chipping away at some of the ideas I've had building up for a few years now. One of the benefits of putting all of this off for so long has been that I have a nice stockpile of concepts from simple on-phone stuff to more complex ideas requiring some server infrastructure to implement (it remains to be seen as to whether I'll ever get to those ones though :P).