Swift: Coping with bad enum typedefs

Earlier this evening, a member of #iphonedev on Freenode needed to use the following function:

func rangeEnclosingPosition(_ position: UITextPosition!,
            withGranularity granularity: UITextGranularity,
                inDirection direction: UITextDirection) -> UITextRange!

The docs say that UITextDirection “can be of type UITextStorageDirection or UITextLayoutDirection.” but when you look this all up, you get:

enum UITextStorageDirection : Int {
    case Forward
    case Backward
}

enum UITextLayoutDirection : Int {
    case Right
    case Left
    case Up
    case Down
}

typealias UITextDirection = Int

As one member of the #swift-lang room put it, “‘My fault or Swift’s fault’ is a pretty frustrating experience.”

What to do? Lily B was able to suggest an immediate workaround by using UITextStorageDirection.Forward.toRaw(), and encouraged filing a radar.

Bigger lesson, especially taking yesterday’s language updates into account: Don’t write production code in a beta language, and make copious notes where you used work-arounds so you can re-address them later.

Comments are closed.