gpt4 book ai didi

ios - 用于开关文本字段的maxLength

转载 作者:行者123 更新时间:2023-12-01 17:46:06 27 4
gpt4 key购买 nike

我有几个文本字段,每个文本字段具有不同数量的最大字符。如何将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
}

最佳答案

 func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let newLength = (textField.text ?? "").count + string.count - range.length
switch textField {
case textFieldA :
return newLength <= 6
case textFieldB:
return newLength <= 7
case textFieldC:
return newLength <= 8
default:
return true
}
}

关于ios - 用于开关文本字段的maxLength,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62174463/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com