处理具有相同签名但参数不同的多个函数的最有效方法

问题描述 投票:0回答:2

当前,我有多个具有相同签名但在被称为base时执行不同操作的函数:

func drawPath(from: JMapDestination, to: JMapDestination) {
    guard let fromWaypoint = navigationManager.getWaypointForDestination(destination: from),
        let toWaypoint = navigationManager.getWaypointForDestination(destination: to) else {
            return
    }

    drawPath(from: fromWaypoint, to: toWaypoint)
}


func drawPathFrom(_ from: CGPoint, to: JMapDestination) {
    guard let fromWaypoint = getWaypointForPoint(point: from),
        let toWaypoint = navigationManager.getWaypointForDestination(destination: to) else {
            return
    }

    drawPath(from: fromWaypoint, to: toWaypoint)
}



func drawPath(from: CGPoint, to: JMapWaypoint) {
    guard let fromWaypoint = getWaypointForPoint(point: from) else { return }
    drawPath(from: fromWaypoint, to: to)
}

我决定使用switch语句创建一个枚举和一个主函数来处理不同的情况:

enum pathType {
    case jMapWaypoint
    case jMapDestination
    case cgPoint
}


func drawPath(pathType: pathType, fromJMap: JMapWaypoint?, toJMap: JMapWaypoint?, fromJDestination: JMapDestination?, toJDestination: JMapDestination?, fromCGPoint: CGPoint?) {

    switch pathType {

    case .jMapWaypoint:
        guard let mapController = viewModel?.mapController else {
            return
        }

        let pathStyle = setPathStyle(style: JMapStyle.init())
        if let from = fromJMap, let to = toJMap {
            checkPathsBetweenWaypoints(mapController: mapController, pathStyle: pathStyle, from: from, to: to)
        }
        if let currentMap = mapController.currentMap {
            mapController.zoomToPath(on: currentMap, withPadding: 100, withAnimationDuration: 1)
        }

    case .jMapDestination:
        if let from = fromJDestination, let to = toJDestination {
            guard let fromWaypoint = getWaypointForDestination(destination: from),
                let toWaypoint = getWaypointForDestination(destination: to) else {
                    return
            }
            drawPath(pathType: .jMapWaypoint, fromJMap: fromWaypoint, toJMap: toWaypoint, fromJDestination: nil, toJDestination: nil, fromCGPoint: nil)
        }

    case .cgPoint:
        if let from = fromCGPoint, let to = toJMap {
            guard let fromWaypoint = getWaypointForPoint(point: from) else { return }
            drawPath(pathType: .jMapWaypoint, fromJMap: fromWaypoint, toJMap: to, fromJDestination: nil, toJDestination: nil, fromCGPoint: nil)
        }
    }
}

switch语句的功能正在运行,但是我想知道是否有更干净,更有效的方法?仅供参考,所有功能都在同一个viewController上,我在考虑协议,但是如果协议功能签名相同(即drawPath)但参数不同,怎么办呢?

ios swift switch-statement overloading
2个回答
0
投票

我不知道它的正确方法...为了方便起见,我将为CGPointJMapDestination创建扩展,它们具有将其转换为JMapWaypoint的方法,称为[ C0]。最后,我将使用扩展方法仅调用最终方法,而不用跳过那么多的障碍。

示例:

func asWaypoint

然后只需将final方法调用为extension CGPoint { func asWaypoint() -> JMapWaypoint { // your logic for the conversion } }

希望这会有所帮助。


0
投票

更干净的版本(IMHO)将是一个包含路径的开始和结束的结构,例如:

drawPath(from: from.asWaypoint(), to: to.asWaypoint())

然后,您可以简单地使用所需的任何类型来初始化此对象,获取最终的所需类型并使用该对象实例绘制路径。

© www.soinside.com 2019 - 2024. All rights reserved.