在swift中将if else结构转换为switch case

时间:2016-10-11 06:27:37

标签: ios swift if-statement switch-statement

我在编程方面很新,我试图将这个if else结构转换为Swift语言中的Switch案例,感谢您的帮助,谢谢。

这是我的代码

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "ShowDefinition") {
        if let destinationViewController = segue.destinationViewController as? EnglishViewController {
            if let definition = sender as? String {
                if definition == "Abstraction" {
                    destinationViewController.titleMsg = "Abstraction"
                    destinationViewController.definitionMsg = "definition 1"
                } else if definition == "Binary System" {
                    destinationViewController.titleMsg = "Binary System"
                    destinationViewController.definitionMsg = "definition 2"
                } else if definition == "Computer" {
                    destinationViewController.titleMsg = "Computer"
                    destinationViewController.definitionMsg = "definition 3"

                } else if definition == "Internet" {
                    destinationViewController.titleMsg = "Internet"
                    destinationViewController.definitionMsg = "definition 4"

                } else if definition == "Virtual Reality" {
                    destinationViewController.titleMsg = "Virtual Reality"
                    destinationViewController.definitionMsg = "definition 5"

                }
            }
        }
    }
}

4 个答案:

答案 0 :(得分:1)

这样的事情:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "ShowDefinition") {
        if let destinationViewController = segue.destinationViewController as? EnglishViewController {
            if let definition = sender as? String {
                switch definition {
                case "Abstraction":
                    destinationViewController.titleMsg = "Abstraction"
                    destinationViewController.definitionMsg = "definition 1"
                case "Binary System":
                    destinationViewController.titleMsg = "Binary System"
                    destinationViewController.definitionMsg = "definition 2"
                case "Computer":
                    destinationViewController.titleMsg = "Computer"
                    destinationViewController.definitionMsg = "definition 3"
                case "Internet":
                    destinationViewController.titleMsg = "Internet"
                    destinationViewController.definitionMsg = "definition 4"
                    destinationViewController.titleMsg = "Abstraction"
                    destinationViewController.definitionMsg = "definition 1"
                case "Virtual Reality":
                    destinationViewController.titleMsg = "Virtual Reality"
                    destinationViewController.definitionMsg = "definition 5"
                default:
                    destinationViewController.titleMsg = ""
                    destinationViewController.definitionMsg = ""
                }
            }
        }
    }
}

检查您要对default值执行的操作,如果case都不匹配,则会触发该值。

答案 1 :(得分:1)

由于titeMsg将始终包含definition字符串,因此您可以缩短代码

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
  if segue.identifier == "ShowDefinition" {
    if let destinationViewController = segue.destinationViewController as? EnglishViewController {
      if let definition = sender as? String {
        switch definition {
        case "Abstraction":
          destinationViewController.definitionMsg = "definition 1"
        case "Binary System":
          destinationViewController.definitionMsg = "definition 2"
        case "Computer":
          destinationViewController.definitionMsg = "definition 3"
        case "Internet":
          destinationViewController.definitionMsg = "definition 4"
        case "Virtual Reality":
          destinationViewController.definitionMsg = "definition 5"
        default :
          destinationViewController.definitionMsg = "n/a"

        }
        destinationViewController.titleMsg = definition
      }
    }
  }
}

你可以更进一步,宣布一个案例的枚举

enum Definitions : String {
  case Abstraction, BinarySystem = "Binary System", Computer, Internet, VirtualReality = "Virtual Reality"
}

这会保存default语句,因为这些案例都是详尽无遗的。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
  if (segue.identifier == "ShowDefinition") {
    if let destinationViewController = segue.destinationViewController as? EnglishViewController {
      if let definition = sender as? String, let definitionEnum = Definitions(rawValue:definition) {
        switch definitionEnum {
        case .Abstraction:
          destinationViewController.definitionMsg = "definition 1"
        case .BinarySystem:
          destinationViewController.definitionMsg = "definition 2"
        case .Computer:
          destinationViewController.definitionMsg = "definition 3"
        case .Internet:
          destinationViewController.definitionMsg = "definition 4"
        case .VirtualReality:
          destinationViewController.definitionMsg = "definition 5"
        }
        destinationViewController.titleMsg = definition
      }
    }
  }
}

答案 2 :(得分:0)

使用此代码

var chart = nv.models.pieChart()
... 
chart.pie.dispatch.on("elementClick", function(e) {
    var pie_sector_name = e.data.label;
    // do something;
});

答案 3 :(得分:0)

在此特定用例中,您不必真正使用序列if-elseswitch块,因为该测试仅适用于测试给定的String实例(definition)等于给定String列表中的任何成员。

由于destinationViewController.titleMsgdefinition的值相同(如果匹配),destinationViewController.definitionMsg只是一个正在运行的数字w.r.t. +1列表的索引(String),您可以在单个可选绑定子句中捕获所有逻辑:

let validTitles = ["Abstraction", "Binary System", "Computer", "Internet", "Virtual Reality"]
let definition = "Abstraction"

if let definitionAndTitle = validTitles.enumerated().filter({$1 == definition}).first {
    print("titleMsg=\(definitionAndTitle.1)")
    print("definitionMsg=\(definitionAndTitle.0 + 1)")
}

应用于您的示例(只是扩展现有的可选绑定子句)

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "ShowDefinition") {
        if let destinationViewController = segue.destinationViewController as? EnglishViewController {
            // or, better, keep this list of titles as e.g. a static
            // immutable property of your viewcontroller class, as these
            // should already be set at compile time.
            let validTitles = ["Abstraction", "Binary System",
                               "Computer", "Internet", "Virtual Reality"]
            if let definition = sender as? String, let defNum = 
                validTitles.enumerated().filter({$1 == definition}).first?.0 {
                destinationViewController.titleMsg = definition
                destinationViewController.definitionMsg = "definition \(defNum+1)"
            }
        }
    }
}