I want to create a max length to my textfield with IBInspectable
, I see a answer to this on a question here but I'm getting an error saying Expression type '()' is ambiguous without more context
,
My code was
import UIKit
private var __maxLengths = [UITextField: Int]()
extension UITextField {
@IBInspectable var maxLength: Int {
get {
guard let l = __maxLengths[self] else {
return 150 // (global default-limit. or just, Int.max)
}
return l
}
set {
__maxLengths[self] = newValue
addTarget(self, action: #selector(fix), for: .editingChanged)
}
}
@objc func fix(textField: UITextField) {
let t = textField.text
textField.text = t?.prefix(maxLength)
}
}
and I'm getting an error pointing at textField.text = t?.prefix(maxLength)
with an error message saying Expression type '()' is ambiguous without more context
,
How can I resolve it?
@objc
annotation in front of func fix? I think it causes this error. – Gnatcatcher@objc
is required – Phytogeography