Swift Compiler Bugs

Swift is a great language, and far more enjoyable to program in than Objective-C, but it is still in its infancy, and bugs abound. One of the more frustrating is compiler errors that are flat-out wrong. Take a look at this (in Xcode 6.1):

Could not find member

“Could not find member ‘date’?” Trust me when I tell you that date is indeed a member of a Todo object. So what’s going on? Let’s tweak it a bit:

Cannot invoke

Ah, there we go. Turns out you can’t actually compare dates like that (hmm…I smell a follow-up post on writing extensions). Here’s how you do it:

Fixed

Here’s another one. The following is essentially a no-op:

for i in 0..<0 {
    println(i)
}

That’s just a for loop from zero up to, but not including, zero. And this code is fine, doing what it is supposed to do—nothing.

However, try making the ending value of the loop less than the starting value. This should be equivalent to the above:

for i in 0...(-1) {
    println(i)
}

The ... operator means “up to and including”. So this should also be a no-op (and it is in most programming languages). But:

For Loop Boom

Even worse, it will actually crash on the first executable line of code:

For Loop Boom 2

None of this should scare you away from using Swift. It’s a fun language to work with, and ends up being much more concise than Objective-C. Just keep in mind that there are still bugs to be worked out, and if you’re getting errors that just make no sense, there’s a fair chance you’ve hit one.