[swift-evolution] [Review] SE-0112: Improved NSError Bridging

Ben Rimmington me at benrimmington.com
Tue Jul 5 19:00:38 CDT 2016


<https://github.com/apple/swift-evolution/blob/master/proposals/0112-nserror-bridging.md>

The new protocols could be combined into a single CustomNSError protocol.
This would mirror the NSError class APIs, which are being customized.

Instead of using NSError.setUserInfoValueProvider(forDomain:provider:) 
could you wrap the CustomNSError value inside an NSError subclass?

	class _CustomNSError: NSError {

	    private let _error: CustomNSError

	    init(_error: CustomNSError) {
	        self._error = _error
	        super.init(
	            domain:   _error.dynamicType.errorDomain,
	            code:     _error.errorCode,
	            userInfo: _error.errorUserInfo)
	    }

	    override var localizedDescription: String {
	        return _error.errorDescription ?? super.localizedDescription
	    }

	    override var localizedFailureReason: String? {
	        return _error.failureReason ?? super.localizedFailureReason
	    }

	    override var localizedRecoverySuggestion: String? {
	        return _error.recoverySuggestion ?? super.localizedRecoverySuggestion
	    }

	    override var localizedRecoveryOptions: [String]? {
	        return _error.recoveryOptions ?? super.localizedRecoveryOptions
	    }

	    override var recoveryAttempter: AnyObject? {
	        if _error.recoveryOptions != nil {
	            return _NSErrorRecoveryAttempter(error: _error)
	        } else {
	            return super.recoveryAttempter
	        }
	    }

	    override var helpAnchor: String? {
	        return _error.helpAnchor ?? super.helpAnchor
	    }
	}

-- Ben



More information about the swift-evolution mailing list