SnapKit 原始碼解讀(二):DSLs

小橘爺發表於2018-05-23

與 Masonry 不同,SnapKit 充分利用了 Swift 的語言特性,用更優雅的方式實現了一套 DSL。而這一切的開始,源於 ConstraintDSL。

ConstraintDSL

ConstraintDSL 是一個協議:

public protocol ConstraintDSL {
    
    var target: AnyObject? { get }
    
    func setLabel(_ value: String?)
    func label() -> String?
    
}
複製程式碼

public

public 的作用是,即使兩部分程式碼處於兩個不同的 module,也能使用這個協議。在尋常的專案中,預設的許可權級別是 internal,即在同一個 module 內可以使用。如果是寫三方庫,則要用 public 來標記提供給外界的 API 了。

協議擴充套件

Objective-C 中的協議不同,Swift 中的協議可以通過擴充套件為協議新增預設的實現,如下所示,為 setLabel ,label 協議方法提供了預設的實現:

extension ConstraintDSL {
    
    public func setLabel(_ value: String?) {
        objc_setAssociatedObject(self.target as Any, &labelKey, value, .OBJC_ASSOCIATION_COPY_NONATOMIC)
    }
    public func label() -> String? {
        return objc_getAssociatedObject(self.target as Any, &labelKey) as? String
    }
    
}
複製程式碼

關聯物件

提供給 ConstraintDSL 協議的預設實現其實是實現了關聯物件的效果,關聯物件是通過擴充套件實現屬性的一種方式,set 方法使用的 API 為:

@available(iOS 3.1, *)
public func objc_setAssociatedObject(_ object: Any, _ key: UnsafeRawPointer, _ value: Any?, _ policy: objc_AssociationPolicy)
複製程式碼

object 參數列示關聯的源物件,在這裡是 self.target as Any,因為 Swift 是一門強型別的語言,所以需要做一個型別轉換。

key 參數列示關聯物件的鍵,SnapKit 是宣告瞭一個 private var labelKey: UInt8 = 0 來作為 key,使用的時候需要配合 & 操作符一起使用。

value 參數列示與物件的關鍵鍵關聯的值。

policy 參數列示關聯的策略,是一個列舉,取值如下:

public enum objc_AssociationPolicy : UInt {

    
    /**< Specifies a weak reference to the associated object. */
    case OBJC_ASSOCIATION_ASSIGN

    /**< Specifies a strong reference to the associated object. 
     *   The association is not made atomically. */
    case OBJC_ASSOCIATION_RETAIN_NONATOMIC

    
    /**< Specifies that the associated object is copied. 
     *   The association is not made atomically. */
    case OBJC_ASSOCIATION_COPY_NONATOMIC

    
    /**< Specifies a strong reference to the associated object.
     *   The association is made atomically. */
    case OBJC_ASSOCIATION_RETAIN

    
    /**< Specifies that the associated object is copied.
     *   The association is made atomically. */
    case OBJC_ASSOCIATION_COPY
}
複製程式碼

Objective-C 屬性的對應關係是一致的,在此不再贅述。

關聯引用的 getAPI 如下:

@available(iOS 3.1, *)
public func objc_getAssociatedObject(_ object: Any, _ key: UnsafeRawPointer) -> Any?
複製程式碼

所需的兩個引數與 set 中的一致。

ConstraintBasicAttributesDSL

ConstraintBasicAttributesDSL 是一個繼承於 ConstraintDSL 的協議,如下所示:

public protocol ConstraintBasicAttributesDSL : ConstraintDSL {
}
複製程式碼

進而又通過擴充套件為其新增了一些計算屬性,會返回物件和其相關的佈局屬性的模型物件,屬於 ConstraintItem 型別,如下所示:

extension ConstraintBasicAttributesDSL {
    
    // MARK: Basics
    
    public var left: ConstraintItem {
        return ConstraintItem(target: self.target, attributes: ConstraintAttributes.left)
    }
    
    public var top: ConstraintItem {
        return ConstraintItem(target: self.target, attributes: ConstraintAttributes.top)
    }
    
    public var right: ConstraintItem {
        return ConstraintItem(target: self.target, attributes: ConstraintAttributes.right)
    }
    
    public var bottom: ConstraintItem {
        return ConstraintItem(target: self.target, attributes: ConstraintAttributes.bottom)
    }
    
    public var leading: ConstraintItem {
        return ConstraintItem(target: self.target, attributes: ConstraintAttributes.leading)
    }
    
    public var trailing: ConstraintItem {
        return ConstraintItem(target: self.target, attributes: ConstraintAttributes.trailing)
    }
    
    public var width: ConstraintItem {
        return ConstraintItem(target: self.target, attributes: ConstraintAttributes.width)
    }
    
    public var height: ConstraintItem {
        return ConstraintItem(target: self.target, attributes: ConstraintAttributes.height)
    }
    
    public var centerX: ConstraintItem {
        return ConstraintItem(target: self.target, attributes: ConstraintAttributes.centerX)
    }
    
    public var centerY: ConstraintItem {
        return ConstraintItem(target: self.target, attributes: ConstraintAttributes.centerY)
    }
    
    public var edges: ConstraintItem {
        return ConstraintItem(target: self.target, attributes: ConstraintAttributes.edges)
    }
    
    public var size: ConstraintItem {
        return ConstraintItem(target: self.target, attributes: ConstraintAttributes.size)
    }
    
    public var center: ConstraintItem {
        return ConstraintItem(target: self.target, attributes: ConstraintAttributes.center)
    }
    
}
複製程式碼

ConstraintAttributesDSL

ConstraintAttributesDSL 是一個繼承於 ConstraintBasicAttributesDSL 的協議,如下所示:

public protocol ConstraintAttributesDSL : ConstraintBasicAttributesDSL {
}
複製程式碼

進而又通過擴充套件為其新增了一些計算屬性,會返回物件和其相關的佈局屬性(baselinemargin 相關)的模型物件,屬於 ConstraintItem 型別,如下所示:

extension ConstraintAttributesDSL {
    
    // MARK: Baselines
    
    @available(*, deprecated:3.0, message:"Use .lastBaseline instead")
    public var baseline: ConstraintItem {
        return ConstraintItem(target: self.target, attributes: ConstraintAttributes.lastBaseline)
    }
    
    @available(iOS 8.0, OSX 10.11, *)
    public var lastBaseline: ConstraintItem {
        return ConstraintItem(target: self.target, attributes: ConstraintAttributes.lastBaseline)
    }
    
    @available(iOS 8.0, OSX 10.11, *)
    public var firstBaseline: ConstraintItem {
        return ConstraintItem(target: self.target, attributes: ConstraintAttributes.firstBaseline)
    }
    
    // MARK: Margins
    
    @available(iOS 8.0, *)
    public var leftMargin: ConstraintItem {
        return ConstraintItem(target: self.target, attributes: ConstraintAttributes.leftMargin)
    }
    
    @available(iOS 8.0, *)
    public var topMargin: ConstraintItem {
        return ConstraintItem(target: self.target, attributes: ConstraintAttributes.topMargin)
    }
    
    @available(iOS 8.0, *)
    public var rightMargin: ConstraintItem {
        return ConstraintItem(target: self.target, attributes: ConstraintAttributes.rightMargin)
    }
    
    @available(iOS 8.0, *)
    public var bottomMargin: ConstraintItem {
        return ConstraintItem(target: self.target, attributes: ConstraintAttributes.bottomMargin)
    }
    
    @available(iOS 8.0, *)
    public var leadingMargin: ConstraintItem {
        return ConstraintItem(target: self.target, attributes: ConstraintAttributes.leadingMargin)
    }
    
    @available(iOS 8.0, *)
    public var trailingMargin: ConstraintItem {
        return ConstraintItem(target: self.target, attributes: ConstraintAttributes.trailingMargin)
    }
    
    @available(iOS 8.0, *)
    public var centerXWithinMargins: ConstraintItem {
        return ConstraintItem(target: self.target, attributes: ConstraintAttributes.centerXWithinMargins)
    }
    
    @available(iOS 8.0, *)
    public var centerYWithinMargins: ConstraintItem {
        return ConstraintItem(target: self.target, attributes: ConstraintAttributes.centerYWithinMargins)
    }
    
    @available(iOS 8.0, *)
    public var margins: ConstraintItem {
        return ConstraintItem(target: self.target, attributes: ConstraintAttributes.margins)
    }
    
    @available(iOS 8.0, *)
    public var centerWithinMargins: ConstraintItem {
        return ConstraintItem(target: self.target, attributes: ConstraintAttributes.centerWithinMargins)
    }
    
}
複製程式碼

ConstraintViewDSL

ConstraintViewDSL 是一個遵守了 ConstraintAttributesDSL 協議的結構體,定義如下:

public struct ConstraintViewDSL: ConstraintAttributesDSL {
    
    ...
    
}
複製程式碼

ConstraintViewDSL 初始化方法只需要一個引數,配合內部的一個屬性 view 儲存需要佈局的檢視:

internal let view: ConstraintView
    
internal init(view: ConstraintView) {
    self.view = view
        
}
複製程式碼

ConstraintViewDSL 真正需要自己實現的代理中的屬性只有一個,就是 target,這是一個用來返回佈局對應的檢視的計算屬性,相關實現如下:

public var target: AnyObject? {
    return self.view
}
複製程式碼

ConstraintViewDSL 宣告瞭一些關鍵方法,內部都是通過呼叫 ConstraintMaker 類的相關方法實現的:

@discardableResult
public func prepareConstraints(_ closure: (_ make: ConstraintMaker) -> Void) -> [Constraint] {
    return ConstraintMaker.prepareConstraints(item: self.view, closure: closure)
}
    
public func makeConstraints(_ closure: (_ make: ConstraintMaker) -> Void) {
    ConstraintMaker.makeConstraints(item: self.view, closure: closure)
}
    
public func remakeConstraints(_ closure: (_ make: ConstraintMaker) -> Void) {
    ConstraintMaker.remakeConstraints(item: self.view, closure: closure)
}
    
public func updateConstraints(_ closure: (_ make: ConstraintMaker) -> Void) {
    ConstraintMaker.updateConstraints(item: self.view, closure: closure)
}
    
public func removeConstraints() {
    ConstraintMaker.removeConstraints(item: self.view)
}
複製程式碼

@discardableResult

有的時候,方法有一個返回值,但我們經常性的會忽視這個返回值,這時可以在方法前加上 @discardableResult 標記,可以在這種情況下不出現警告。

最後,宣告瞭一些計算屬性,作為一些設定 view 相關屬性的 shortcut

public var contentHuggingHorizontalPriority: Float {
    get {
        return self.view.contentHuggingPriority(for: .horizontal).rawValue
    }
    set {
        self.view.setContentHuggingPriority(LayoutPriority(rawValue: newValue), for: .horizontal)
    }
}
    
public var contentHuggingVerticalPriority: Float {
    get {
        return self.view.contentHuggingPriority(for: .vertical).rawValue
    }
    set {
        self.view.setContentHuggingPriority(LayoutPriority(rawValue: newValue), for: .vertical)
    }
}
    
public var contentCompressionResistanceHorizontalPriority: Float {
    get {
        return self.view.contentCompressionResistancePriority(for: .horizontal).rawValue
    }
    set {
        self.view.setContentCompressionResistancePriority(LayoutPriority(rawValue: newValue), for: .horizontal)
    }
}
    
public var contentCompressionResistanceVerticalPriority: Float {
    get {
        return self.view.contentCompressionResistancePriority(for: .vertical).rawValue
    }
    set {
        self.view.setContentCompressionResistancePriority(LayoutPriority(rawValue: newValue), for: .vertical)
    }
}
複製程式碼

ConstraintLayoutGuideDSL

ConstraintLayoutGuideDSL 是一個遵守了 ConstraintAttributesDSL 協議的結構體,作用是替 UILayoutGuide 提供了一些 DSLs,與 ConstraintViewDSL 唯一的區別在於,target 屬性返回的物件不同:

public struct ConstraintLayoutGuideDSL: ConstraintAttributesDSL {
    
    ...
    
    public var target: AnyObject? {
        return self.guide
    }
    
    internal let guide: ConstraintLayoutGuide
    
    internal init(guide: ConstraintLayoutGuide) {
        self.guide = guide
        
    }
    
}
複製程式碼

ConstraintLayoutSupportDSL

ConstraintLayoutSupportDSL 是一個遵守了 ConstraintDSL 協議的結構體,作用是替 ConstraintLayoutSupport 提供了一些 DSLs

@available(iOS 8.0, *)
public struct ConstraintLayoutSupportDSL: ConstraintDSL {
    
    public var target: AnyObject? {
        return self.support
    }
    
    internal let support: ConstraintLayoutSupport
    
    internal init(support: ConstraintLayoutSupport) {
        self.support = support
        
    }
    
    public var top: ConstraintItem {
        return ConstraintItem(target: self.target, attributes: ConstraintAttributes.top)
    }
    
    public var bottom: ConstraintItem {
        return ConstraintItem(target: self.target, attributes: ConstraintAttributes.bottom)
    }
    
    public var height: ConstraintItem {
        return ConstraintItem(target: self.target, attributes: ConstraintAttributes.height)
    }
}
複製程式碼

原文地址:SnapKit 原始碼解讀(二):DSLs

如果覺得我寫的還不錯,請關注我的微博@小橘爺,最新文章即時推送~

相關文章