Skip to content

[SDK] Get the preferred @encode string for MapKit types dynamically #19691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions stdlib/public/SDK/CoreLocation/NSValue.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,37 @@
import Foundation

%{
from gyb_foundation_support import ObjectiveCBridgeableImplementationForNSValue
from gyb_foundation_support import \
ObjectiveCBridgeableImplementationForNSValueWithCategoryMethods
}%

${ ObjectiveCBridgeableImplementationForNSValue("CLLocationCoordinate2D") }
// Get the ObjC type used by -[NSValue valueWithMKCoordinate:]
// to instantiate the resulting NSValue objects, in case these get changed
// in the future. This is extra tricky because MapKit *might not be loaded*,
// in which case we need to fall back to the static @encode string.
private let CLLocationCoordinate2DInNSValueObjCType: UnsafePointer<CChar> = {
let factorySel = Selector(("valueWithMKCoordinate:"))
guard let opaqueFactoryMethod = NSValue.method(for: factorySel) else {
return _getObjCTypeEncoding(CLLocationCoordinate2D.self)
}
typealias FactoryMethodType =
@convention(c) (AnyClass, Selector, CLLocationCoordinate2D) -> NSValue
let factoryMethod =
unsafeBitCast(opaqueFactoryMethod, to: FactoryMethodType.self)
return factoryMethod(NSValue.self, factorySel, .init()).objCType
}()

${ ObjectiveCBridgeableImplementationForNSValueWithCategoryMethods(
Type="CLLocationCoordinate2D",
initializer="""{
var addressableValue = $0
return NSValue(bytes: &addressableValue,
objCType: CLLocationCoordinate2DInNSValueObjCType)
}""",
getter="""{
var result = CLLocationCoordinate2D()
$0.getValue(&result)
return result
}""",
objCType="{ _ in CLLocationCoordinate2DInNSValueObjCType }",
) }
32 changes: 30 additions & 2 deletions stdlib/public/SDK/MapKit/NSValue.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,35 @@
import Foundation

%{
from gyb_foundation_support import ObjectiveCBridgeableImplementationForNSValue
from gyb_foundation_support import \
ObjectiveCBridgeableImplementationForNSValueWithCategoryMethods
}%

${ ObjectiveCBridgeableImplementationForNSValue("MKCoordinateSpan") }
// Get the ObjC type used by -[NSValue valueWithMKCoordinateSpan:]
// to instantiate the resulting NSValue objects, in case these get changed
// in the future.
@available(tvOS 9.2, *)
private let MKCoordinateSpanInNSValueObjCType =
NSValue(mkCoordinateSpan: .init()).objCType

${ ObjectiveCBridgeableImplementationForNSValueWithCategoryMethods(
Type="MKCoordinateSpan",
initializer="""{
guard #available(tvOS 9.2, *) else {
fatalError("MKCoordinateSpan is not supported on tvOS before tvOS 9.2")
}
return NSValue(mkCoordinateSpan: $0)
}""",
getter="""{
guard #available(tvOS 9.2, *) else {
fatalError("MKCoordinateSpan is not supported on tvOS before tvOS 9.2")
}
return $0.mkCoordinateSpanValue
}""",
objCType="""{ _ in
guard #available(tvOS 9.2, *) else {
fatalError("MKCoordinateSpan is not supported on tvOS before tvOS 9.2")
}
return MKCoordinateSpanInNSValueObjCType
}""",
) }
8 changes: 4 additions & 4 deletions test/stdlib/MapKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ func spansEqual(_ x: MKCoordinateSpan, _ y: MKCoordinateSpan)
}

if #available(tvOS 9.2, *) {
mapKit.test("NSValue bridging")
.xfail(.iOSMinor(9, 3, reason: "<rdar://problem/41440036>"))
.xfail(.osxMinorRange(10, 9...10, reason: "<rdar://problem/44866579>"))
.code {
mapKit.test("CLLocationCoordinate2D bridging") {
expectBridgeToNSValue(CLLocationCoordinate2D(latitude: 17, longitude: 38),
nsValueInitializer: { NSValue(mkCoordinate: $0) },
nsValueGetter: { $0.mkCoordinateValue },
equal: coordinatesEqual)
}

mapKit.test("MKCoordinateSpan bridging") {
expectBridgeToNSValue(MKCoordinateSpan(latitudeDelta: 6,
longitudeDelta: 79),
nsValueInitializer: { NSValue(mkCoordinateSpan: $0) },
Expand Down