Swiftpack.co - mattgallagher/CwlPreconditionTesting as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by mattgallagher.
mattgallagher/CwlPreconditionTesting 2.1.2
A Mach exception handler that allows Swift precondition failures to be caught and tested.
⭐️ 169
🕓 25 weeks ago
.package(url: "https://github.com/mattgallagher/CwlPreconditionTesting.git", from: "2.1.2")


A Mach exception handler, written in Swift and Objective-C, that allows EXC_BAD_INSTRUCTION (as raised by Swift's assertionFailure/preconditionFailure/fatalError) to be caught and tested.

NOTE: the iOS code runs in the simulator only. It is for logic testing and cannot be deployed to the device due to the Mach exception API being private on iOS.

For an extended discussion of this code, please see the Cocoa with Love article:

Partial functions in Swift, Part 2: Catching precondition failures


From version 2.0.0-beta.1, building CwlPreconditionTesting requires Swift 5 or newer and the Swift Package Manager, or CocoaPods.

For use with older versions of Swift or other package managers, use version 1.2.0 or older.

Adding to your project

Swift Package Manager

Add the following to the dependencies array in your "Package.swift" file:

 .package(url: "https://github.com/mattgallagher/CwlPreconditionTesting.git", from: Version("2.0.0"))

Or by adding https://github.com/mattgallagher/CwlPreconditionTesting.git, version 2.0.0 or later, to the list of Swift packages for any project in Xcode.


CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate CwlPreconditionTesting into your Xcode project using CocoaPods, specify it in your Podfile:

pod 'CwlPreconditionTesting', '~> 2.0'


On macOS and iOS you can use the regular version:

import CwlPreconditionTesting

let e = catchBadInstruction {

on tvOS, Linux and other platforms, you can use the POSIX version:

import CwlPosixPreconditionTesting

let e = catchBadInstruction {

Warning: this POSIX version can't be used when lldb is attached since lldb's Mach exception handler blocks the SIGILL from ever occurring. You should disable the "Debug Executable" setting for the tests in Xcode. The POSIX version of the signal handler is also whole process (rather than correctly scoped to the thread where the "catch" occurs).


Includes contributions from @abbeycode, @dnkoutso, @jeffh and @ikesyo. Extra thanks to @saagarjha for help with the ARM64 additions.


Stars: 169
Last commit: 2 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics