What does “get” mean in a protocol's property declaration?

倾然丶 夕夏残阳落幕 提交于 2019-12-17 20:28:58

问题


I'm looking at some code from an auto layout library. In it, there is a protocol adopted by 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 }
}

What does an empty { get } accomplish?


回答1:


These are properties for which the classes that adopt the protocol must supply a getter. The protocol does not specify anything about the setter, so classes could supply a computed property instead of a stored one.

For example, a class that adopts Constrainable could satisfy a requirement of having topAnchor by adding

var topAnchor: NSLayoutYAxisAnchor

or by adding

var topAnchor: NSLayoutYAxisAnchor {
    ...
    return ...
}


来源:https://stackoverflow.com/questions/44059548/what-does-get-mean-in-a-protocols-property-declaration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!