MihaelIsaev / UIKitPlus

🏰 Declarative UIKit with LivePreview for iOS9+ (best alternative to SwiftUI)
MIT License
596 stars 35 forks source link

Configure `UView` with closure #26

Closed markst closed 2 years ago

markst commented 2 years ago

Trying to work out if the following might be possible:

extension UView {
  @discardableResult
  func `do`<T: UView>(_ closure: (T) -> Void) -> T {
    closure(self)
    return self
  }
}

So that view's can be configured such as:

Preview {
  ShowCollectionViewCell(frame: .zero)
    .do { $0.viewModel = .init(identity: "123", title: "test") }
    .centerYInSuperview()
markst commented 2 years ago

Could be relevant: https://github.com/devxoul/Then

markst commented 2 years ago

Not sure what the opinion is on this:

extension AnyDeclarativeProtocol where Self: Any {
  @inlinable
  public func `do`(_ block: (inout Self) throws -> Void) rethrows -> Self {
    var copy = self
    try block(&copy)
    return copy
  }
}
EkkoG commented 2 years ago

You can use apply to do this https://github.com/MihaelIsaev/UIKitPlus/blob/86cee195d2ba27f877ec0c5c59dae2060b4f001a/Classes/Extensions/DeclarativeProtocol%2BApply.swift#L14

markst commented 2 years ago

not sure how I missed that. @EkkoG thanks!