|
2 | 2 | import Foundation
|
3 | 3 |
|
4 | 4 | %{
|
5 |
| -from gyb_foundation_support import ObjectiveCBridgeableImplementationForNSValue |
| 5 | +from gyb_foundation_support import \ |
| 6 | + ObjectiveCBridgeableImplementationForNSValueWithCategoryMethods |
6 | 7 | }%
|
7 | 8 |
|
8 |
| -${ ObjectiveCBridgeableImplementationForNSValue("CLLocationCoordinate2D") } |
| 9 | +// Get the ObjC type used by -[NSValue valueWithMKCoordinate:] |
| 10 | +// to instantiate the resulting NSValue objects, in case these get changed |
| 11 | +// in the future. This is extra tricky because MapKit *might not be loaded*, |
| 12 | +// in which case we need to fall back to the static @encode string. |
| 13 | +private let CLLocationCoordinate2DInNSValueObjCType: UnsafePointer<CChar> = { |
| 14 | + let factorySel = Selector(("valueWithMKCoordinate:")) |
| 15 | + guard let opaqueFactoryMethod = NSValue.method(for: factorySel) else { |
| 16 | + return _getObjCTypeEncoding(CLLocationCoordinate2D.self) |
| 17 | + } |
| 18 | + typealias FactoryMethodType = |
| 19 | + @convention(c) (AnyClass, Selector, CLLocationCoordinate2D) -> NSValue |
| 20 | + let factoryMethod = |
| 21 | + unsafeBitCast(opaqueFactoryMethod, to: FactoryMethodType.self) |
| 22 | + return factoryMethod(NSValue.self, factorySel, .init()).objCType |
| 23 | +}() |
| 24 | + |
| 25 | +${ ObjectiveCBridgeableImplementationForNSValueWithCategoryMethods( |
| 26 | + Type="CLLocationCoordinate2D", |
| 27 | + initializer="""{ |
| 28 | + var addressableValue = $0 |
| 29 | + return NSValue(bytes: &addressableValue, |
| 30 | + objCType: CLLocationCoordinate2DInNSValueObjCType) |
| 31 | + }""", |
| 32 | + getter="""{ |
| 33 | + var result = CLLocationCoordinate2D() |
| 34 | + $0.getValue(&result) |
| 35 | + return result |
| 36 | + }""", |
| 37 | + objCType="{ _ in CLLocationCoordinate2DInNSValueObjCType }", |
| 38 | +) } |
0 commit comments