ios - 重构圈复杂度大于 10 的 switch 语句

标签 ios swift cycle cyclomatic-complexity

我有这个代码:

for notif in EnvironmentManager.notif {
    if let type = notif.type {
            switch type {
            case .SitterAvailable:
                self.manageNotif(notif, title: "Une sitter disponible", storyboardName: "searchGuard", vcName: "searchGuardNCSID")
            case .OccasionalAdjustmentReminder:
                self.manageNotif(notif, title: "Rappel", storyboardName: "searchGuard", vcName: "searchGuardNCSID")
            case .GuardRequest:
                self.manageNotif(notif, title: "Nouvelle garde urgente", storyboardName: "EmergencyGuardSitter", vcName: "EmergencyGuardSitterNavigationControllerSID")
            case .NewReservationRequest:
                self.manageNotif(notif, title: "Nouvelle garde", storyboardName: "GuardWebSitter", vcName: "WebGuardSitterNavigationControllerSID")
            case .NewMessage:
                self.manageNotif(notif, title: "Nouveau message", storyboardName: "MessageListSitter", vcName: "messageSitterViewNavigationControllerSID")
            case .SoonReservationStartReminder:
                self.manageNotif(notif, title: "Rappel", storyboardName: "GuardListSitter", vcName: "guardListSitterNavigationControllerSID")
            case .ReservationAccepted:
                self.manageNotif(notif, title: "Garde acceptée", storyboardName: "GuardListSitter", vcName: "guardListSitterNavigationControllerSID")
            case .ReservationRefused:
                self.manageNotif(notif, title: "Garde refusée", storyboardName: "GuardListSitter", vcName: "guardListSitterNavigationControllerSID")
            case .NewMessageParent:
                self.manageNotif(notif, title: "Nouveau Message", storyboardName: "MessageParent", vcName: "messageParentViewNavigationControllerSID")
            }
        }
    }

我想知道如何优化它以对抗圈复杂度, 所以没有字符串数组或类似的东西,

当前复杂度等于 11

感谢帮助

最佳答案

包含 title、storyboardName 和 vcName 的简单结构数组可以非常巧妙地解决这个问题。

如果类型从 0..10 开始排序,那么您可以只使用数组中的索引。如果 type 不是从 0..10 开始运行,您可以将其包含在结构中,然后首先找到您需要的索引。

像这样定义你的结构

struct NotificationTypeData
{
    var notificationType    : Int    = 0
    var title               : String = ""
    var storyboardName      : String = ""
    var vcName              : String = ""
}

然后设置一个变量来保存它

var notificationTypeData : [NotificationTypeData]?

您需要从文件或硬编码中填充它。这是前几个...

notificationTypeData!.append(NotificationTypeData(notificationType: 0, title: "Une sitter disponible", storyboardName: "searchGuard", vcName: "searchGuardNCSID"))
notificationTypeData!.append(NotificationTypeData(notificationType: 1, title: "Rappel", storyboardName: "EmergencyGuardSitter", vcName: "EmergencyGuardSitterNavigationControllerSID"))
notificationTypeData!.append(NotificationTypeData(notificationType: 2, title: "Nouvelle garde urgente", storyboardName: "GuardWebSitter", vcName: "WebGuardSitterNavigationControllerSID"))
notificationTypeData!.append(NotificationTypeData(notificationType: 3, title: "Nouvelle garde", storyboardName: "MessageListSitter", vcName: "messageSitterViewNavigationControllerSID"))
notificationTypeData!.append(NotificationTypeData(notificationType: 4, title: "Nouveau message", storyboardName: "GuardListSitter", vcName: "guardListSitterNavigationControllerSID"))

然后简化您已有的开关代码

for notif in EnvironmentManager.notif
{
    if let type = notif.type
    {
         switch type
         {
             let data = notificationTypeData!.filter{ $0.notificationType == type }.first
             if data != nil
             {
                 self.manageNotif(notif, title: data?.title, storyboardName: data?.storyboardName, vcName: data?.storyboardName
             }
         }
     }

关于ios - 重构圈复杂度大于 10 的 switch 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39370278/

相关文章:

时间轴上的日期模周

ios - 如何回到我的项目 Xcode 10 的旧版本

仅 iOS 7 应用程序在启动时崩溃

iphone - PreventAppSheet(仅在邮件中使用)- iOS 邮件负载配置

ios - Xcode: Storyboard太困惑且“忙碌”

python - 生成循环谱

ios - 如何使navigationItem.searchController的searchBar使用屏幕的整个宽度

swift - 如何在 where 子句中使用协议(protocol)扩展

xcode - Date 中的字符串有效,String 中的日期无效

javascript - jQuery 第一次循环没有延迟