Latest 1.1
Homepage https://github.com/swifterror/swerror
License Apache-2.0
Platforms ios 12.1, osx 10.14

SwiftError

SwiftError allows you to instantiate errors that conform to Error, CustomNSError and LocalizedError without having to declare an error type of your own.

Conveniences

Two utility functions are provided as examples that demonstrate how to streamline source code by leveraging error types.

  • cast<T, U>(_ subject: T) -> U

    Never force cast when working with remote APIs again. Instead, let string = any as! String becomes

    let string = try cast(any) as String
  • unwrap<T>(_ optional: T?) -> T

    Rather than force unwrap optionals while working with legacy interfaces, rewrite
    let some = optional! as

    let some = try unwrap(optional)

Demo

  1. Compile the demo
    swiftc SwiftError/*.swift main.swift -o demo
  2. Run the demo
    ./demo

Sample output

Demo print output
-----------------
Undefined error -10001, demoThrow(), main.swift:L5
["key": "val", "float": 3.1, "int": 256]

Demo debugPrint output
----------------------
Undefined error -10001, demoThrow(), main.swift:L5
0   main                                0x000000010f1be165 $S4main9demoThrowyyKF + 501
1   main                                0x000000010f1bc96d main + 45
{
    float = "3.1";
    int = 256;
    key = val;
}

Demo cast print output
----------------------
Unexpected String in place of Int -10000, demoCast(), main.swift:L22

Demo cast debugPrint output
---------------------------
Unexpected String in place of Int -10000, demoCast(), main.swift:L22
0   main                                0x000000010f1bf015 $S4main4cast_4file8function4lineq_x_S2SSitKr0_lF + 1189
1   main                                0x000000010f1be2f9 $S4main8demoCastyyKF + 217
2   main                                0x000000010f1bca55 main + 277

Demo unwrap print output
------------------------
Unexpected nil for optional Any -9999, demoUnwrap(), main.swift:L40

Demo unwrap debugPrint output
-----------------------------
Unexpected nil for optional Any -9999, demoUnwrap(), main.swift:L40
0   main                                0x000000010f1be9fb $S4main6unwrap_4file8function4linexxSg_S2SSitKlF + 891
1   main                                0x000000010f1be464 $S4main10demoUnwrapyyKF + 212
2   main                                0x000000010f1bcb3d main + 509

Latest podspec

{
    "name": "Swerror",
    "version": "1.1",
    "summary": "Swift Error type",
    "source": {
        "git": "https://github.com/SwiftError/Swerror.git",
        "tag": "1.1"
    },
    "authors": "Ilias Karim",
    "license": "Apache-2.0",
    "homepage": "https://github.com/swifterror/swerror",
    "swift_version": "4.2",
    "platforms": {
        "ios": "12.1",
        "osx": "10.14"
    },
    "source_files": "Swerror/*.swift"
}

Pin It on Pinterest

Share This