Skip to content

Instantly share code, notes, and snippets.

@leviathan
Last active July 20, 2021 13:03
Show Gist options
  • Save leviathan/f878d57e567809862a7a8d7817158821 to your computer and use it in GitHub Desktop.
Save leviathan/f878d57e567809862a7a8d7817158821 to your computer and use it in GitHub Desktop.
2021-07-26 - Bosch eBike CoP

Combine syntactic sugar

Defining a Publisher pipeline, that Never fails:

Before
let diamonds: AnyPublisher<Diamond, Never>
After
let diamonds: Flow<Diamond>

Type-erasing a Never failing Publisher pipeline to AnyPublisher:

Before
let diamonds: AnyPublisher<Diamond, Never> = 
    Just(Diamond(carat: 7))
        .eraseToAnyPublisher()
After
let diamonds: Flow<Diamond> = 
    Just(Diamond(carat: 7))
        .eraseToFlow()

In place editing

Edit newly created entities right where they are defined...

Before
final class DiamondView: UIView {
    @UsesAutoLayout private var titleLabel = UILabel(style: .title2)
    
    // MARK: Initializer
    
    override init(frame: CGRect = .zero) {
        super.init(frame: frame)
        setupViews()
    }
    
    // MARK: Private
    
    private func setupViews() {
        titleLabel.textAlignment = .center
        titleLabel.allowsDefaultTighteningForTruncation = true
        titleLabel.adjustsFontSizeToFitWidth = true
        titleLabel.minimumScaleFactor = 0.5
    }
}
After
final class DiamondView: UIView {
    @UsesAutoLayout private var titleLabel = UILabel(style: .title2).then {
        $0.textAlignment = .center
        $0.allowsDefaultTighteningForTruncation = true
        $0.adjustsFontSizeToFitWidth = true
        $0.minimumScaleFactor = 0.5
    }
    
    // MARK: Initializer
    
    override init(frame: CGRect = .zero) {
        super.init(frame: frame)
    }
}

Replace "helper" properties with in-place-setup:

Before
private var buttonConstraint: NSLayoutConstraint?

private func setupConstraints() {
    buttonConstraint = heightAnchor.constraint(equalToConstant: 16)
    buttonConstraint?.priority = .defaultHigh
    buttonConstraint?.isActive = true
    
    NSLayoutConstraint.activate([
        // other constraint setup ..
    ])
}
After
private func setupConstraints() {    
    NSLayoutConstraint.activate([
        heightAnchor.constraint(equalToConstant: 16).then {
            $0.priority = .defaultLow
        },        
        // other constraint setup ..
    ])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment