Created
September 9, 2016 20:40
-
-
Save sammoore/f07459480d86b0bc7011fefc391ccf2f to your computer and use it in GitHub Desktop.
A (simpler) protocol to implement than _ObjectiveCBridgeable which implicitly implements it. Tested with Swift 2.2, Xcode 7.3.1. SourceKit tends to have problems parsing this (it will cause crashes) -- edits welcome, although I haven't taken a look in a bit.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import protocol Foundation.NSObjectProtocol | |
/// The minimum requirements for bridging conforming Swift "named types" | |
/// into a representable Objective-C type, i.e. a subclass of NSObject. | |
/// | |
/// Note that implementing the direct descendents of `ObjCBridgeable` | |
/// provides automatic conformance to `_ObjectiveCBridgeable`. | |
protocol ObjCBridgeable: _ObjectiveCBridgeable { | |
// Provides code-completion since the latter is hidden, and provides | |
// an extremely reasonable restriction which the standard library lacks. | |
// | |
// Note: the reason this is missing is possibly due to the ability to | |
// convert to C structs and related types, a feature which was vastly | |
// improved with Swift 2.3/3.0. | |
associatedtype _ObjectiveCType: NSObjectProtocol | |
static func _objc_metatype() -> _ObjectiveCType.Type | |
init?(_objc_bridge: _ObjectiveCType) | |
var _objc_bridge: _ObjectiveCType { get } | |
} | |
extension ObjCBridgeable // _ObjectiveCBridgeable | |
{ | |
static func _isBridgedToObjectiveC() -> Bool { return true } | |
static func _getObjectiveCType() -> Any.Type { | |
return _objc_metatype() | |
} | |
func _bridgeToObjectiveC() -> Self._ObjectiveCType { | |
return self._objc_bridge | |
} | |
static func _forceBridgeFromObjectiveC( | |
source: Self._ObjectiveCType, | |
inout result: Self? | |
) { | |
result = Self.init(_objc_bridge: source) | |
} | |
static func _conditionallyBridgeFromObjectiveC( | |
source: Self._ObjectiveCType, | |
inout result: Self? | |
) -> Bool { | |
_forceBridgeFromObjectiveC(source, result: &result) | |
return true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment