不确定如何避免 Swift 开关的默认语句中的垃圾
Not sure how to avoid junk in default statement of Swift switch
Switch 语句在 Swift 中必须是详尽无遗的,因此我必须在默认语句中添加一些内容以使编译器满意。
private enum TextFieldType: Int {
case street1 = 0
case street2
case street3
case town
case postcode
case phone
case email
}
@IBAction func textFieldEditingChanged(textField: UITextField, forEvent event: UIEvent) {
switch textField.tag {
case TextFieldType.street1.rawValue:
self.model?.address1 = textField.text
case TextFieldType.street2.rawValue:
self.model?.address2 = textField.text
case TextFieldType.street3.rawValue:
self.model?.address3 = textField.text
case TextFieldType.town.rawValue:
self.model?.town = textField.text
case TextFieldType.postcode.rawValue:
self.model?.postcode = textField.text
case TextFieldType.phone.rawValue:
self.model?.primaryPhone = textField.text
case TextFieldType.email.rawValue:
self.model?.email = textField.text
default:
println("")
}
}
在这种情况下(如上),您将如何处理默认语句?为了它而放置 println("") 是不正确的。
您应该使用 break
离开开关语句:
default:
break
Switch 语句在 Swift 中必须是详尽无遗的,因此我必须在默认语句中添加一些内容以使编译器满意。
private enum TextFieldType: Int {
case street1 = 0
case street2
case street3
case town
case postcode
case phone
case email
}
@IBAction func textFieldEditingChanged(textField: UITextField, forEvent event: UIEvent) {
switch textField.tag {
case TextFieldType.street1.rawValue:
self.model?.address1 = textField.text
case TextFieldType.street2.rawValue:
self.model?.address2 = textField.text
case TextFieldType.street3.rawValue:
self.model?.address3 = textField.text
case TextFieldType.town.rawValue:
self.model?.town = textField.text
case TextFieldType.postcode.rawValue:
self.model?.postcode = textField.text
case TextFieldType.phone.rawValue:
self.model?.primaryPhone = textField.text
case TextFieldType.email.rawValue:
self.model?.email = textField.text
default:
println("")
}
}
在这种情况下(如上),您将如何处理默认语句?为了它而放置 println("") 是不正确的。
您应该使用 break
离开开关语句:
default:
break