【发布时间】:2020-06-03 04:30:24
【问题描述】:
我想用IBInspectable 为我的文本字段创建一个最大长度,我在here 问题上看到了这个问题的答案,但我收到了一个错误提示Expression type '()' is ambiguous without more context,
我的代码是
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)
}
}
我收到指向 textField.text = t?.prefix(maxLength) 的错误消息,并显示 Expression type '()' is ambiguous without more context,
我该如何解决?
【问题讨论】:
-
你能去掉func fix前面的
@objc注解吗?我认为它会导致此错误。 -
@Sung-JongWillKim
@objc是必填项 -
哦,选择器已链接。
标签: swift uitextfield maxlength ibinspectable