Does anyone have any experience working with ReferenceConvertible
? I noticed that there isn't a lot of documentation or literature surrounding that protocol (and the even more obscure _ObjectiveCBridgable
protocol). A number of types use it: UrlRequest, Data etc. It seems really useful just thinking about how URLRequest
and NSURLRequest
are somewhat interchangable because of it.
consider this example:
struct Value: ReferenceConvertible {
func _bridgeToObjectiveC() -> MutableValue {
return MutableValue(int)
}
static func _forceBridgeFromObjectiveC(_ source: MutableValue, result: inout Value?) {
//Question: What happens here? Not Documented anywhere
}
static func _conditionallyBridgeFromObjectiveC(_ source: MutableValue, result: inout Value?) -> Bool {
return true //Question: What is this function used for? Not documented anywhere
}
static func _unconditionallyBridgeFromObjectiveC(_ source: MutableValue?) -> Value {
return Value(int: source!.int) // Question: Why is the source nil here? This isn't documented anywhere
}
typealias ReferenceType = MutableValue
typealias _ObjectiveCType = MutableValue //Not documented anywhere, is this a correct implementation?
var description: String { return "" }
var debugDescription: String { return "" }
var int: Int
}
class MutableValue: NSObject, NSCopying {
var int: Int
init(_ int: Int) {
self.int = int
}
func copy(with zone: NSZone? = nil) -> Any {
return self // Is this correct?
}
}
let value = Value(int: 0)
print(value.int)// 0
let mutable = value as MutableValue
mutable.int += 1
let new = mutable as Value
print(new.int)// 1
I guess the underscore that prefixes those functions is trying to tell someone maybe it isn't a good idea to implement these functions. Cause doesn't an underscore traditionally mean private method or method that shouldn't be used normally?