DuctTape 0.2.0

DuctTape 0.2.0

Taiki Suzuki 维护。




DuctTape 0.2.0

DuctTape

CI Status Version License Platform SwiftPM Carthage Language

📦Swift 的 KeyPath dynamicMemberLookup 的语法糖。

let label = UILabel().ductTape
    .numberOfLines(0)
    .textColor(.red)
    .text("Hello, World!!")
    .build()

上方的定义与下方的定义相同。

let label: UILabel = {
    let label = UILabel()
    label.numberOfLines = 0
    label.textColor = .red
    label.text = "Hello, World!!"
    return label
}()

用法

NSObject 已经与 DuctTape 兼容,因此您可以像以下那样访问 .ductTape 属性。

let builder: Builder<UIView> = UIView().ductTape

如果访问 .ductTape,它将返回一个 Builder,该 Build提供通过 KeyPath dynamicMemberLookup 设置属性的 setter。

let view: UIView = UIView().ductTape
    .backgroundColor(.red)
    .translatesAutoresizingMaskIntoConstraints(false)
    .build()

最后,如果调用 .build()Builder 将返回已设置属性值的实例。

如果某些对象与 DuctTape 不兼容,有两种使用 DuctTape 的方法。

  1. 使用 DuctTapeCompatible
class Dog: DuctTapeCompatible {
    var name: String
}

let dog = Dog().ductTape
    .name("Copernicus")
    .build()
  1. 直接使用 Builder
class Dog {
    var name: String
}

let dog = Builder(Dog())
    .name("Copernicus")
    .build()

示例

class ViewController: UIViewController {

    let flowLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        .ductTape
        .minimumLineSpacing(10)
        .minimumInteritemSpacing(10)
        .itemSize(CGSize(width: 100, height: 100))
        .scrollDirection(.vertical)
        .build()

    lazy var collectionView: UICollectionView = UICollectionView(frame: .zero,
                                                                 collectionViewLayout: flowLayout)
        .ductTape
        .dataSource(self)
        .delegate(self)
        .translatesAutoresizingMaskIntoConstraints(false)
        .reinforce {
            $0.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
        }
        .build()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(collectionView)
        NSLayoutConstraint.activate([
            view.topAnchor.constraint(equalTo: collectionView.topAnchor),
            view.leadingAnchor.constraint(equalTo: collectionView.leadingAnchor),
            view.trailingAnchor.constraint(equalTo: collectionView.trailingAnchor),
            view.bottomAnchor.constraint(equalTo: collectionView.bottomAnchor)
        ])
    }
}

要求

  • Xcode 11
  • macOS 10.10
  • iOS 9.0
  • tvOS 10.0
  • watchOS 3.0

安装

CocoaPods

DuctTape可通过CocoaPods获取。要安装,只需将以下行添加到您的Podfile

pod "DuctTape"

Carthage

如果您使用的是Carthage,只需将DuctTape添加到您的Cartfile

github "marty-suzuki/DuctTape"

Swift包管理工具

只需将以下行添加到您的Package.swift

.package(url: "https://github.com/marty-suzuki/DuctTape.git", from: "version")

许可证

DuctTape遵循MIT许可证发布。