gpt4 book ai didi

ios - 检查类型并转换为具体类型以进行功能签名

转载 作者:行者123 更新时间:2023-12-01 18:03:51 25 4
gpt4 key购买 nike

我有以下代码,感觉可以改进。具体来说,我想摆脱转换和强制转换。多边形具有以下类型签名:
let polygon: MKShape & MKGeoJSONObject

switch polygon {
case is MKMultiPolygon:
let multiPoly = polygon as! MKMultiPolygon
renderer = MKMultiPolygonRenderer(multiPolygon: multiPoly)
case is MKPolygon:
let polygon = polygon as! MKPolygon
renderer = MKPolygonRenderer(polygon: polygon)
default:
fatalError("Should have had a renderer")
}

最佳答案

您应该保持切换方法,并使用case let将对象转换为适当的类型:

switch polygon {
case let object as MKMultiPolygon:
renderer = MKMultiPolygonRenderer(multiPolygon: object)
case let object as MKPolygon:
renderer = MKPolygonRenderer(polygon: object)
default:
fatalError("Should have had a renderer")
}

关于ios - 检查类型并转换为具体类型以进行功能签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60106392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com