/NSErrorPointerWrapper

Wrapper for handling iOS SDK APIs that take in error by reference.

Primary LanguageSwiftMIT LicenseMIT

NSErrorPointerWrapper

Wrapper for handling iOS SDK APIs that take in error by reference.

Some of the benefits:

  • Simplified handling of methods that take in NSErrorPoint.
  • No more declaring that pesky NSError variable to pass by reference (use $0 shorthand argument name in closure passed to the wrapper).
  • Chaining handlers for success and failure.
  • Streamlined downcasting handling (needed because often the result of those methods is AnyObject? instance).

Usage

tryWithErrorPointer { NSJSONSerialization.JSONObjectWithData(data, options: nil, error: $0) }
                .onError { error in /* handle error */ }
                .onSuccess { result in /* handle success */ } }

With casting:

tryWithErrorPointer(castResultTo: NSDictionary.self) { NSJSONSerialization.JSONObjectWithData(data, options: nil, error: $0) }
                .onError { error in /* handle error */ }
                .onSuccess { result in /* handle success */ } }

For more details check tests and those resources:

Installation with CocoaPods

  • You need to use CocoaPods version that supports Frameworks: at least 0.36.0. Currently it's a beta feature, to install it update CocoaPods: gem install cocoapods --prerelease.
  • Just add the following line to the Podfile and run pod update:
pod 'NSErrorPointerWrapper'