您能帮我把这些if
语句转换成带有案子的switch
吗?
我苦苦挣扎了一段时间,我不知道该怎么做。
代码如下:
class Settings: NSObject {
let name: SettingsName
let imageName: String
init(name: SettingsName, imageName: String){
self.name = name
self.imageName = imageName
}
}
enum SettingsName: String {
case settings = "Settings"
case terms = "Terms & privacy policy"
case feedback = "Send Feedback"
case help = "Help"
case switchAccount = "Switch Account"
case cancel = "Cancel"
}
@objc func handleDismiss(setting: Settings) {
UIView.animate(withDuration: 0.5, animations: {
self.blackView.alpha = 0
if let window = UIApplication.shared.keyWindow {
self.collectionView.frame = CGRect(x: 0, y: window.frame.height, width: self.collectionView.frame.width, height: self.collectionView.frame.height)
}
}, completion: { (_) in
if setting.name == .settings {
self.homeController?.showControllerForAccountSettings(setting: setting)
}
if setting.name == .terms {
self.homeController?.showControllerForTermsAndPrivacy(setting: setting)
}
else if setting.name != .cancel && setting.name != .settings{
self.homeController?.showDummyControllerForSetting(setting: setting)
}
})
}
答案 0 :(得分:3)
您似乎想这样做:
switch settings.name {
case .settings:
self.homeController?.showControllerForAccountSettings(setting: setting)
case .terms:
self.homeController?.showControllerForTermsAndPrivacy(setting: setting)
case .cancel:
break
default:
self.homeController?.showDummyControllerForSetting(setting: setting)
}