1

在 xCode 更新 6.3 之后,我遇到了这个问题:“MyUItextViewExtension.swift:xx:xx: 方法 'editingRectForBounds' 与 Objective-C 选择器 'editingRectForBounds:' 与先前声明与相同的 Objective-C 选择器冲突”

我的斯威夫特代码是:

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 上的另一类扩展...

4

3 回答 3

4

editingRectForBounds已经存在于 UITextField 中,所以您可能应该override使用它。

文档中:

您不应直接调用此方法。如果要为文本提供不同的编辑矩形,可以覆盖此方法并返回该矩形。

编辑:

我之前没有意识到您想在扩展中使用它。同样,如果我相信文档并且这次我没有弄错您想要的东西,那么就有问题了:

您不能使用扩展来覆盖 Objective-C 类型上的现有方法或属性。

于 2015-04-13T15:00:03.370 回答
1

添加到现有的答案 - Xcode 6.3 和 Swift 1.2 对你被允许做什么和不做什么更加严格。Apple 已迅速收紧,您很可能已经摆脱了一些“糟糕”但以前版本的 Xcode 没有发现的问题。

于 2015-04-13T17:49:27.597 回答
0

可以重写一个函数,但它必须声明为公共的。错误是

覆盖实例方法必须与其覆盖的声明一样可访问

这是我一直致力于扩展 UITextField 的示例

override public func becomeFirstResponder() -> Bool
于 2016-01-28T16:00:30.207 回答