P. 58: fatalError()

The text says,

But, this app:

[code]import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

@IBOutlet weak var window: NSWindow!


func applicationDidFinishLaunching(aNotification: NSNotification) {
     myFunc()
}

func applicationWillTerminate(aNotification: NSNotification) {
    // Insert code here to tear down your application
}

func myFunc() -> String {
    let x = 3
    assert(x==2, "Bad value for x")
}

}
[/code]

does not produce any compiler errors or warnings. I can use assert() or fatalError() interchangeably inside myFunc(), and they appear to have the same effect. I’m using Xcode 6.3.2.

Good question. This is one of those areas that we wanted to give the reader a taste of, but fully explaining it gets into territory that is not appropriate for this point in the book. This is also not well documented (last I checked). :frowning:

There is a big difference between fatalError() and assert(). By default, in the Release build configuration, assert()s are not compiled into your application. These lines become no-ops. So if you try building this app in Release, the compiler will error on this line. (You can try this using Product -> Build For -> Profiling - by default the Profile action builds Release.)

fatalError() is not left out by the compiler, which means that you can use it to terminate execution in any build configuration.

Does that help?