简体   繁体   中英

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

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?

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 ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM