我正在看自动布局库中的一些代码。其中,UIView采用了一个协议:
extension UIView: Constrainable {} extension UILayoutGuide: Constrainable { // LayoutGuide doesn't have baseline anchors, so just use the bottom anchor public var firstBaselineAnchor: NSLayoutYAxisAnchor { return bottomAnchor } public var lastBaselineAnchor: NSLayoutYAxisAnchor { return bottomAnchor } } public protocol Constrainable { var topAnchor: NSLayoutYAxisAnchor { get } var bottomAnchor: NSLayoutYAxisAnchor { get } var leftAnchor: NSLayoutXAxisAnchor { get } var rightAnchor: NSLayoutXAxisAnchor { get } var leadingAnchor: NSLayoutXAxisAnchor { get } var trailingAnchor: NSLayoutXAxisAnchor { get } var centerXAnchor: NSLayoutXAxisAnchor { get } var centerYAnchor: NSLayoutYAxisAnchor { get } var widthAnchor: NSLayoutDimension { get } var heightAnchor: NSLayoutDimension { get } var firstBaselineAnchor : NSLayoutYAxisAnchor { get } var lastBaselineAnchor : NSLayoutYAxisAnchor { get } }
一个空虚的{ get }成就是什么?
{ get }
这些是采用协议的类必须为其提供吸气剂的属性。该协议未指定有关setter的任何内容,因此类可以提供计算的属性,而不是存储的属性。
例如,采用的类Constrainable可以topAnchor通过添加来满足具有的要求
Constrainable
topAnchor
var topAnchor: NSLayoutYAxisAnchor
或通过添加
var topAnchor: NSLayoutYAxisAnchor { ... return ... }