【发布时间】:2020-09-22 05:56:09
【问题描述】:
我有几个文本字段,每个字段的最大字符数不同。如何将 if 分支更改为枚举并使用 switch?
//if -> switch
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let newLength = (textField.text ?? "").count + string.count - range.length
if(textField == textFieldA) {
return newLength <= 6
}
if(textField == textFieldB) {
return newLength <= 7
}
if(textField == textFieldC) {
return newLength <= 8
}
return true
}
【问题讨论】:
标签: ios swift if-statement switch-statement conditional-statements