Swiftpack.co - johnno1962/Fortify as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by johnno1962.
johnno1962/Fortify 2.1.8
Making Swift more robust
⭐️ 95
🕓 2 weeks ago
.package(url: "https://github.com/johnno1962/Fortify.git", from: "2.1.8")

Fortify - A more robust Swift

Fortify is a small Swift Package that allows Swift to be more robust in the face of what are currently fatal errors such as forced unwrap of nil or casting errors.

Use Fortify as you would existing Swift exceptions except that exceptions are generated automatically for Swift errors. Errors are thrown up the stack manually without intermediate functions being declared as throwing. An example:

import Fortify

do {
    try Fortify.protect {
        var a: String!
        a = a!
} catch {
    NSLog("Unwrap error: \(error)")

In the example, the forced unwrap of nil will generate an exception rather than terminate the application. To see it running in Xcode, you need run the example app outside the debugger.


This form of exception throwing is rather ill-mannered in that it will not clean up live objects, operations or system resources in use in intermediate frames. This approach is suggested where it is important not to crash such as in development of Swift application servers. No representation about the reliability of an application once an error has occurred is made though in general the undesirable effects should be limited to leaking of memory and system resources. A more detailed write-up of how Fortify works and its use of setjmp/longjmp is here.


Stars: 95
Last commit: 2 weeks ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

6 years ago

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