Skip to content

Instantly share code, notes, and snippets.

@bsneed
Created January 15, 2020 02:39
Show Gist options
  • Save bsneed/ecc8524db97855e80768328bda6b4cdb to your computer and use it in GitHub Desktop.
Save bsneed/ecc8524db97855e80768328bda6b4cdb to your computer and use it in GitHub Desktop.
An example of bridging, and mutable/immutable class sets.
public struct AContext: ReferenceConvertible, Equatable, Hashable {
internal var _handle: SEGMutableContext
public typealias ReferenceType = SEGContext
public typealias _ObjectiveCType = SEGContext
public static func _getObjectiveCType() -> Any.Type {
return SEGContext.self
}
public var debugDescription: String = "dookie"
public var description: String = "poop"
init(analytics: SEGAnalytics) {
_handle = SEGMutableContext(analytics: analytics)
}
init(_ context: SEGContext) {
_handle = context.mutableCopy()
}
fileprivate init(_bridge context: SEGContext) {
_handle = context.mutableCopy()
}
@_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> SEGContext {
return _handle as SEGContext
}
public static func _forceBridgeFromObjectiveC(_ source: SEGContext, result: inout AContext?) {
result = AContext(_bridge: source)
}
public static func _conditionallyBridgeFromObjectiveC(_ source: SEGContext, result: inout AContext?) -> Bool {
result = AContext(_bridge: source)
return true
}
public static func _unconditionallyBridgeFromObjectiveC(_ source: SEGContext?) -> AContext {
var result: AContext? = nil
_forceBridgeFromObjectiveC(source!, result: &result)
return result!
}
public var debug: Bool {
get {
return _handle.debug
}
set {
_handle.debug = newValue
}
}
}
let mutable = AContext(context)
mutable.debug = true // COMPILER ERROR. GOOD!
//----------
var mutable = AContext(context)
mutable.debug = true // WORKS!
//----------
var mutable = SEGContext(analytics: analytics) as AContext
mutable.debug = true // WORKS!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment