在xCode更新6.3之后,我遇到了这个问题:"MyUItextViewExtension.swift:xx:xx:带有Objective-C选择器的'editingRectForBounds‘方法'editingRectForBounds:’与以前使用相同的Objective-C选择器的声明冲突“
我的Swift代码是:
extension UITextField{
func editingRectForBounds(bounds: CGRect) -> CGRect{
if self.leftView != nil {
let textFieldPadding : CGFloat = 24.0
return CGRectMake(bounds.origin.x + textFieldPadding, bounds.origin.y, bounds.size.width+textFieldPadding*2, bounds.size.height)
} else {
return textRectForBounds(bounds)
}
}
func textRectForBounds(bounds: CGRect) -> CGRect {
if self.leftView != nil {
let textFieldPadding : CGFloat = 24.0
return CGRectMake(bounds.origin.x + textFieldPadding, bounds.origin.y, bounds.size.width+textFieldPadding*2, bounds.size.height)
} else {
let textFieldPadding : CGFloat = 8.0
return CGRectMake(bounds.origin.x + textFieldPadding, bounds.origin.y, bounds.size.width+textFieldPadding*2, bounds.size.height)
}
}
}我不明白这个错误的原因。在项目中没有在UITextField上定义另一个扩展类别...
发布于 2015-04-13 23:00:03
UITextField中已经存在editingRectForBounds,因此您可能应该对其执行override操作。
不应直接调用此方法。如果要为文本提供不同的编辑矩形,可以重写此方法并返回该矩形。
编辑:
我之前没有意识到你想在一个扩展中使用它。再说一次,如果我相信documentation,如果这次我没有弄错你想要什么,那就有问题了:
不能使用扩展来覆盖Objective-C类型的现有方法或属性。
发布于 2015-04-14 01:49:28
补充现有答案- Xcode 6.3和Swift 1.2在允许和不允许做什么方面有更严格的要求。苹果已经收紧了swift,很可能你会用一些“坏”的东西逃脱惩罚,但以前的Xcode版本没有赶上。
发布于 2016-01-29 00:00:30
你可以用重写一个函数,但它必须声明为。错误是
重写实例方法必须与它重写的声明一样具有可访问性
下面是我用来扩展UITextField的一个例子
override public func becomeFirstResponder() -> Boolhttps://stackoverflow.com/questions/29608478
复制相似问题