Skip to content
Ken Harris edited this page Oct 9, 2020 · 6 revisions

Conventional wisdom is that you should never assume a compiler bug when something goes wrong, but Swift is still (even as of version "5") very young and you will almost certainly run into compiler bugs. Here's a list of a few of the gotchas I've run into.

  • Don't use didSet in a recursive data structure. As of Swift 4, it will be silently skipped. (SR-419) (fixed in Swift 5)

  • Don't override ~= for matching of your custom types. It's a neat idea (Ruby and other languages have similar features) but in many cases it simply doesn't work. (SR-1121)

  • Don't write an extension with a convenience init, for an init that throws. It'll compile, but cause a double-free at runtime. (SR-3637) (fixed in Swift 5?)

  • There are places you simply can't put a comment. It's easy to break SwiftUI due to this. Maybe just always put them on their own line, to be safe. (SR-13139)

Documentation

There's many places which are maybe not technically bugs, but you'd never know it from the documentation.

  • Implicitly unwrapped optionals (IUOs) are supposed to act like non-optionals, but there's many situations where they don't. For example, as the value in a switch, they're not unwrapped.
  • The "Summary of the Grammar" at docs.swift.org is all kinds of wrong. For example, it says the only place you can put a comment (as of Swift 5.x) is inside a string literal, which is (literally) the only place you cannot put a comment.

Use

  • You can't write Quick Look plugins in Swift, at least as of 10.14. The language is supposed to be compatible with Objective-C, but it requires runtime support, and QL runs plugins in the same memory space, so even if you link a Swift runtime yourself, there's no way to guarantee it won't smash the symbols of some other QL plugin with a different version.

    • I can't tell if it's possible to write a Spotlight importer in Swift. (The first StackOverflow answer assumes it's possible, for reasons which make no sense, and never got it working. The second StackOverflow answer starts with an Objective-C target, imports the Swift functionality after it's been built separately, and then edits the project file ... it doesn't look a standard approach.) You can create a target for it and choose "Language: Swift", but it gives you a template for a CSIndexExtensionRequestHandler, which seems to be something completely different.

      • After filing a DTS issue, I've come to the conclusion that (a) it's almost certainly possible to write an importer in Swift, but (b) nearly impossible, to the point where a programmer using Apple's current tools and reading the public documentation would never in a million years guess what all is required. Your best bet is to download a copy of Xcode 8 and use that Objective-C template, and/or file a ticket with DTS and have them help you through it.

Subset

See Swift subset.

Clone this wiki locally