You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A simple Swift wrapper around an Objective-C `@try`/`@catch` statement that selectively catches Objective-C exceptions by `NSException` subtype, rethrowing if any caught exception is not the expected subtype.
3
+
4
+
Look at [CwlCatchExceptionTests.swift](https://github.com/mattgallagher/CwlCatchException/blob/master/Tests/CwlCatchExceptionTests/CwlCatchExceptionTests.swift) for syntax.
5
+
6
+
## Requirements
7
+
8
+
From version 2.0.0-beta.1, building CwlCatchException requires Swift 5 or newer and the Swift Package Manager.
9
+
10
+
For use with older versions of Swift or other package managers, [use version 1.2.0 or older](https://github.com/mattgallagher/CwlCatchException/tree/1.2.0).
11
+
12
+
## Adding to your project
13
+
14
+
Add the following to the `dependencies` array in your "Package.swift" file:
Or by adding `https://github.com/mattgallagher/CwlCatchException.git`, version 2.0.0-beta.1 or later, to the list of Swift packages for any project in Xcode.
Copy file name to clipboardexpand all lines: Carthage/Checkouts/CwlPreconditionTesting/README.md
+23-35
Original file line number
Diff line number
Diff line change
@@ -8,52 +8,40 @@ For an extended discussion of this code, please see the Cocoa with Love article:
8
8
9
9
[Partial functions in Swift, Part 2: Catching precondition failures](http://cocoawithlove.com/blog/2016/02/02/partial-functions-part-two-catching-precondition-failures.html)
10
10
11
-
## Adding to your project
12
-
13
-
This project can be used by manual inclusion in your projects or through any of the Swift Package Manager, CocoaPods or Carthage.
14
-
15
-
Minimum requirements are iOS 8 (simulator-only) or macOS 10.9. The project includes tvOS 9 and POSIX targets but these aren't regularly tested.
16
-
17
-
### Manual inclusion
18
-
19
-
1. In a subdirectory of your project's directory, run `git clone https://github.com/mattgallagher/CwlPreconditionTesting.git`
20
-
2. Drag the "CwlPreconditionTesting.xcodeproj" file from the Finder into your own project's file tree in Xcode
21
-
3. Add the "CwlPreconditionTesting.framework" from the "Products" folder of the CwlPreconditionTesting project's file tree to the "Copy Files (Frameworks)" build phases of any targets that you want to include this module.
22
-
4. Drag the "CwlCatchException.framework" from the "Dependencies" group (within the CwlPreconditionTesting project's file tree) onto the same "Copy Files (Frameworks)" build phase
11
+
## Requirements
23
12
24
-
### Swift Package Manager
13
+
From version 2.0.0-beta.1, building CwlPreconditionTesting requires Swift 5 or newer and the Swift Package Manager.
25
14
26
-
Assuming you're using the `swift-tools-version:4.0` package manager, add the following to the `dependencies` array in your "Package.swift" file:
15
+
For use with older versions of Swift or other package managers, [use version 1.2.0 or older](https://github.com/mattgallagher/CwlPreconditionTesting/tree/1.2.0).
> NOTE: even though this git repository includes its dependencies in the Dependencies folder, building via the Swift Package manager fetches and builds these dependencies independently.
31
-
32
-
### CocoaPods
33
-
34
-
Add the following lines to your target in your "Podfile":
17
+
## Adding to your project
35
18
36
-
pod 'CwlPreconditionTesting', :git => 'https://github.com/mattgallagher/CwlPreconditionTesting.git'
37
-
pod 'CwlCatchException', :git => 'https://github.com/mattgallagher/CwlCatchException.git'
19
+
Add the following to the `dependencies` array in your "Package.swift" file:
Or by adding `https://github.com/mattgallagher/CwlPreconditionTesting.git`, version 2.0.0-beta.1 or later, to the list of Swift packages for any project in Xcode.
For comparison or for anyone running this code on a platform without Mach exceptions or the Objective-C runtime, I've added a proof-of-concept implementation of `catchBadInstruction` that uses a POSIX SIGILL `sigaction` and `setjmp`/`longjmp` to perform the throw.
29
+
```swift
30
+
importCwlPreconditionTesting
48
31
49
-
In Xcode, you can simply select the CwlPreconditionTesting_POSIX target (instead of the OSX or iOS targets). If you're building without Xcode: all you need is the CwlCatchBadInstructionPOSIX.swift file (compared to the Mach exception handler, the code is tiny doesn't have any weird Objective-C/MiG file dependencies).
32
+
let e =catchBadInstruction {
33
+
precondition(false, "THIS PRECONDITION FAILURE IS EXPECTED")
34
+
}
35
+
```
50
36
51
-
**Warning No. 1**: on OS X, this approach can't be used when lldb is attached since lldb's Mach exception handler blocks the SIGILL from ever occurring (I've disabled the "Debug Executable" setting for the tests in Xcode - re-enable it to witness the problem).
37
+
on tvOS, Linux and other platforms, you can use the POSIX version:
52
38
53
-
**Warning No. 2**: if you're switching between the CwlPreconditionTesting_OSX and CwlPreconditionTesting_POSIX targets, Xcode (as of Xcode 7.2.1) will not detect the change and will not remove the old framework correctly so you'll need to *clean your project* otherwise the old framework will hang around.
39
+
```swift
40
+
importCwlPosixPreconditionTesting
54
41
55
-
Additional problems in decreasing severity include:
42
+
let e =catchBadInstruction {
43
+
precondition(false, "THIS PRECONDITION FAILURE IS EXPECTED")
44
+
}
45
+
```
56
46
57
-
* the signal handler is whole process (rather than correctly scoped to the thread where the "catch" occurs)
58
-
* the signal handler doesn't deal with re-entrancy whereas the mach exception handler remains deterministic in the face of multiple fatal errors
59
-
* the signal handler overwrites the "[red zone](https://en.wikipedia.org/wiki/Red_zone_(computing))" which is technically frowned upon in signal handlers (although unlikely to cause problems here)
47
+
**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).
Copy file name to clipboardexpand all lines: Carthage/Checkouts/CwlPreconditionTesting/Sources/CwlPosixPreconditionTesting/CwlCatchBadInstructionPosix.swift
+1-1
Original file line number
Diff line number
Diff line change
@@ -18,7 +18,7 @@
18
18
// IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
0 commit comments