我有这段代码:
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
感谢您的帮助
答案 0 :(得分:0)
包含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
}
}
}