Swift协议:隐藏一些属性

时间:2016-02-24 14:18:20

标签: swift swift-protocols

我想为trimmedTextUITextView创建UITextField属性。这是我做的:

protocol TrimmedTextSupporting: class {
  var _text: String? { get }
  var trimmedText: String { get }
}

extension TrimmedTextSupporting {
  var trimmedText: String {
    let text = self._text ?? ""
    return text.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
  }
}

extension UITextField: TrimmedTextSupporting {

  var _text: String? {
    return self.text
  }
}

extension UITextView: TrimmedTextSupporting {

  var _text: String? {
    return self.text
  }
}

我需要_text属性,因为text中的String?UITextField中被声明为String!UITextView中被private声明为什么(为什么?!> _≤)。现在我想隐藏这个属性以避免混乱API。

这是我试过的:

1)将其标记为'private' modifier cannot be used in protocols。编译器不允许这样做:private protocol TextExposing { var _text: String? { get } } extension UITextField: TextExposing { var _text: String? { return self.text } } extension UITextView: TextExposing { var _text: String? { return self.text } } /////// protocol TrimmedTextSupporting: class { var trimmedText: String { get } } extension UITextField: TrimmedTextSupporting {} extension UITextView: TrimmedTextSupporting {} extension TrimmedTextSupporting where Self: TextExposing { // compiler error var trimmedText: String { let text = self._text ?? "" return text.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) } }

2)将其分成私人协议:

Property 'trimmedText' must be declared internal because it matches a requirement in internal protocol 'TrimmedTextSupporting'

但编译器再次抱怨: def get_next_by_field_filtered(obj, field=None, **kwargs): next_obj = getattr(obj, 'get_next_by_{}'.format(field))() for key in kwargs: if not getattr(next_obj, str(key)) == kwargs[str(key)]: return get_next_by_field_filtered(next_obj, field=field, **kwargs) return next_obj

我没有想法。

3 个答案:

答案 0 :(得分:2)

我倾向于选择:

protocol TrimmedTextSupporting: class {
    var trimmedText: String { get }
}

extension TrimmedTextSupporting {

    private func trimText(text: String) -> String {
        return text.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
    }
}

extension UITextField: TrimmedTextSupporting {

    var trimmedText: String {
        return trimText(text ?? "")
    }
}

extension UITextView: TrimmedTextSupporting {

    var trimmedText: String {
        return trimText(text ?? "")
    }
}

因此,您可以通过在共享和私有功能中执行此操作来避免重复工作,UITextFieldUITextView上的扩展可以做到最低限度。

答案 1 :(得分:0)

您将TrimmedTextSupporting声明为内部协议。如果您不想将trimmedText声明为internal,请将TrimmedTextSupporting声明为私有协议:

private protocol TrimmedTextSupporting: class {
    var trimmedText: String { get }
}

编译好。

let textView = UITextView()
textView.text = "hello  "
print(textView.trimmedText) // "hello"

let textField = UITextField()
textField.text = "  world "
print(textField.trimmedText) // "world"

答案 2 :(得分:-1)

或者你可以不需要trimmedText,设置为可选。

(如果你想在Swift中使用optional,你应该让protocol成为@objc)

@objc protocol TrimmedTextSupporting: class {
     optional var trimmedText: String { get }
}