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).
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:
- NSErrorPointerWrapper: Simplified handling of Cocoa Touch API errors in Swift,
- sample app: swift-objc.io-issue-10-core-data-network-application.
- 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 runpod update
:
pod 'NSErrorPointerWrapper'