I have created a protocol with extension and set the the textfield method properly but when I created the custom class , I fetched the value but not able to send back to the protocol.
protocol Limiter {
var limit : Int {get set}
}
extension UIViewController: UITextFieldDelegate{
public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let tf = textField.text ?? ""
guard let stringRange = Range(range, in: tf) else { return false }
// add their new text to the existing text
let updatedText = tf.replacingCharacters(in: stringRange, with: string)
print(updatedText)
return updatedText.count <= (textField as! Limiter).limit
}
}
//Custom class
class TextFieldLimiter: UITextField , Limiter{
var limit: Int = 0
@IBInspectable var maxLength: Int {
get {
print(limit)
return limit
}
set {
limit = newValue
print(limit)
}
}
}
I tried this code and am not sure I'm doing correctly or not.
So, please help me out...
Thank you in advance