首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重构圈复杂度高于10的开关语句

重构圈复杂度高于10的开关语句
EN

Stack Overflow用户
提问于 2016-09-07 12:42:46
回答 1查看 1.3K关注 0票数 0

我有这样的代码:

代码语言:javascript
复制
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

谢谢你的帮助

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-07 13:02:19

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

如果类型排序为0..10,则只需将索引用于数组。如果类型不从0..10运行,则可以将其包含在结构中,并首先找到所需的索引。

像这样定义你的结构

代码语言:javascript
复制
struct NotificationTypeData
{
    var notificationType    : Int    = 0
    var title               : String = ""
    var storyboardName      : String = ""
    var vcName              : String = ""
}

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

代码语言:javascript
复制
var notificationTypeData : [NotificationTypeData]?

您将需要填充它-从一个文件,或硬编码。这是第一批..。

代码语言:javascript
复制
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"))

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

代码语言:javascript
复制
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
             }
         }
     }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39370278

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档