本文介绍了“得到"是什么意思?在协议的属性声明中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在查看自动布局库中的一些代码.其中有一个UIView采用的协议:
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 } }空的 { get } 有什么作用?
推荐答案这些是采用协议的类必须为其提供 getter 的属性.该协议没有指定有关 setter 的任何内容,因此类可以提供计算属性而不是存储属性.
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.
例如,一个采用Constrainable的类可以通过添加
For example, a class that adopts Constrainable could satisfy a requirement of having topAnchor by adding
var topAnchor: NSLayoutYAxisAnchor或通过添加
var topAnchor: NSLayoutYAxisAnchor { ... return ... }更多推荐
“得到"是什么意思?在协议的属性声明中是什么意思?
发布评论