How to write a generic apply() function in Swift?

后端 未结 4 1431
你的背包
你的背包 2020-12-16 11:45

Is there any way to get the following working in Swift 3?

 let button = UIButton().apply {
        $0.setImage(UIImage(named: \"UserLocation\"), for: .normal         


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-16 12:40

    I had the same issue and ended up solving it with an operator:

    infix operator <-< : AssignmentPrecedence
    func <-<(left:T, right:(T)->()) -> T
    {
      right(left)
      return left
    }
    
    let myObject = UIButton() <-< { $0.isHidden = false }
    

提交回复
热议问题