You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用函数或Switch优化14个多条件图片渲染的if/else判断?

优化多条件图片渲染逻辑的方案

问题背景

需要处理14个if/else条件的图片渲染逻辑,所有分支除了category对应的十六进制颜色值和图标不同外,其余代码完全一致,希望通过函数或Switch语句重构优化。

当前冗余代码示例:

if placeC.places[0].category == "Route" {
    Image("map-pin")
        .renderingMode(.template)
        .resizable()
        .frame(width: 35, height: 35)
        .foregroundColor(placeC.places[0].show ? .red : Color.init(UIColor(hexString: "#6A798E")))
        .onTapGesture {
            if !placeC.places[0].show {
                tapOnMapAnnotation(
                    place: placeC.places[0]
                )
            }
        }
        .overlay(
            Awesome.Image(icon: Awesome.Solid.route)
                .size(12)
                .foregroundColor(.white)
                .padding(.bottom, 15)
        )
} else if placeC.places[0].category == "Scenic Area" {
    Image("map-pin")
        .renderingMode(.template)
        .resizable()
        .frame(width: 35, height: 35)
        .foregroundColor(placeC.places[0].show ? .red : Color.init(UIColor(hexString: "#006994")))
        .onTapGesture {
            if !placeC.places[0].show {
                tapOnMapAnnotation(
                    place: placeC.places[0]
                )
            }
        }
        .overlay(
            Awesome.Image(icon: Awesome.Solid.binoculars)
                .size(12)
                .foregroundColor(.white)
                .padding(.bottom, 15)
        )
// 剩余12个else if分支...

优化方案

1. 定义配置结构体

把每个分类对应的可变参数封装成结构体,统一管理所有分类的配置:

import SwiftUI
import Awesome // 假设这是你使用的图标库

struct PlaceCategoryConfig {
    let inactiveColorHex: String
    let icon: Awesome.Solid
}

2. 实现配置映射逻辑

用switch语句实现分类到配置的映射(类型更安全,适合多分支场景):

func getCategoryConfig(for category: String) -> PlaceCategoryConfig? {
    switch category {
    case "Route":
        return PlaceCategoryConfig(inactiveColorHex: "#6A798E", icon: .route)
    case "Scenic Area":
        return PlaceCategoryConfig(inactiveColorHex: "#006994", icon: .binoculars)
    // 依次添加剩余12个分类的配置
    case "Restaurant":
        return PlaceCategoryConfig(inactiveColorHex: "#FF6B6B", icon: .utensils)
    // ...其他分类
    default:
        return nil // 处理未知分类的默认情况
    }
}

3. 重构渲染代码

复用公共逻辑,仅注入不同的配置参数:

// 先取出当前place,简化后续代码
let currentPlace = placeC.places[0]

if let config = getCategoryConfig(for: currentPlace.category) {
    Image("map-pin")
        .renderingMode(.template)
        .resizable()
        .frame(width: 35, height: 35)
        .foregroundColor(currentPlace.show ? .red : Color(UIColor(hexString: config.inactiveColorHex)))
        .onTapGesture {
            if !currentPlace.show {
                tapOnMapAnnotation(place: currentPlace)
            }
        }
        .overlay(
            Awesome.Image(icon: config.icon)
                .size(12)
                .foregroundColor(.white)
                .padding(.bottom, 15)
        )
} else {
    // 可选:添加未知分类的默认渲染视图
    Image("map-pin-default")
        .resizable()
        .frame(width: 35, height: 35)
}

额外优化建议

  • 如果category是固定可选值,建议将String类型改为自定义枚举(比如enum PlaceCategory: String),避免字符串硬编码错误,同时编译器会提示未处理的枚举分支,提升代码严谨性。
  • 可将渲染逻辑封装为独立View组件(比如PlaceAnnotationView),直接传入currentPlace和点击回调,进一步提升代码复用性。

内容的提问来源于stack exchange,提问作者DevSem

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 23:55:31