devxoul / Then

✨ Super sweet syntactic sugar for Swift initializers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to use this syntax for IBOutlet views?

dinarajas opened this issue · comments

Instead of creating a new instance for a view, is there a possibility to fetch the existing instance and set attributes to it? I want to update attributes initially for IBOutlets views.

@DineshRajaS you can use it with your IBOutlets, no new instance is created. Have a look at how then is implemented:

    func then(block: Self -> Void) -> Self {
        block(self)
        return self
    }

With Outlets you can simply:

self.myOutlet.then { // do stuff }

@DineshRajaS, thank you for asking.
Since IBOutlet variables are initialized lately, you cannot use then with the property declarations. However, as @RuiAAPeres mentioned, you can use then just after the IBOutlets are created.

@DineshRajaS for outlets it is very nice to use didSet property observer for that purpose. It will be called every time you set the property. First time it will be called by the system when instantiating view from xib/storyboard.

@IBOutlet weak var button: UIButton! {
  didSet {
    //set button attributes
  }
}

+1 on @ilyapuchka's solution, that's what I've been doing too. This can probably be closed - I don't think we're going to get anything better than that anytime soon