Two Nifty Swift Loop Tricks

This week, I learnt two things about Swift that I’d never come across before. Both involve loops.

The first, via Erica Sadun: you can use case let in a for loop to conditionally bind optionals or cast items. Here’s the optional binding example from Erica’s post:

let items: [String?] = [nil, nil, "Hello", nil, "World"]
for case let item? in items {
    print(item)
}

Check out the full post, 3 simple for-in iteration tricks for some other neat tricks.

Secondly, from this post at KrakenDev: you can label loops in Swift! Here’s an example:

sectionLoop: for section in sections {
    rowLoop: for row in rows {
        if row.isMagical {
            break sectionLoop
        }
    }
}

Who knew?! There are a bunch more useful tips in the full post, Hipster Swift, including descriptions of what @noescape and @autoclosure actually do.

Fixing Xcode’s Invisible Cursor

When writing code, I generally like to use a dark theme in my IDE or text editor. For Xcode, I really like the Tomorrow Night and Seti themes in particular (both of which can be easily installed using the Alcatraz package manager).

In Xcode, however, there’s a slight problem for dark theme fans:

xcode_beam.png

By default the ‘i-beam’ mouse cursor in the editor is really hard to see, particularly on a high resolution monitor. I’d often find myself losing it and having to shake the mouse to activate El Capitan’s mouse zoom feature.

But there’s a solution! I noticed that Terminal.app’s i-beam cursor has a stronger shadow, which makes it easier to see on dark backgrounds. The cursors are just .tiff image files, so it’s trivial to steal Terminal’s cursor and stick it into Xcode.

If you want to do it manually, you’ll need to copy /Applications/Utilities/Terminal.app/Contents/Resources/ShadowedIBeam.tiff over the top of /Applications/Xcode.app/Contents/SharedFrameworks/DVTKit.framework/Versions/A/Resources/DVTIbeamCursor.tiff. Or you can just run this snippet in Terminal, which will do it for you:

cd /Applications/Xcode.app/Contents/SharedFrameworks/DVTKit.framework/Versions/A/Resources; sudo mv DVTIbeamCursor.tiff DVTIbeamCursor.old; sudo cp /Applications/Utilities/Terminal.app/Contents/Resources/ShadowedIBeam.tiff DVTIbeamCursor.tiff

The change in shadow is actually only slight, but I find it makes a big difference in helping me locate the cursor:

terminal_beam.png

And here’s a before and after:

before_and_after_beams.png

Update: On Twitter, @TwoLivesLeft pointed out that iTerm’s cursor has even better contrast:

Update 2:Also on Twitter, GregHeo, there’s a super-mega visible cursor available on Github: https://github.com/egold/better-xcode-ibeam-cursor