visc
visc

Reputation: 4959

How do I assign a callback handler with inline method in swift?

Here is how I implement an inline callback assignment in Objective-C:

self.beforeAdjustViews = ^ (UIView* btnView)
{
    // do something
};

How do I implement the same thing in Swift 5?

Upvotes: 0

Views: 154

Answers (2)

Ehab Ashraf
Ehab Ashraf

Reputation: 36

You can either assign directly

self.beforeAdjustViews = { (button) in 
    // Write Some Code
}

or create a reference variable to this call back - usually better when you have a lot of properties or trying to keep you code simple and beautiful

let beforAdjustViewsCallBack: (btnView: UIView) -> Void = { button in 
    // Write Some Code
}

Also keep in mind that the same rules for retain cycles are applied, So if you don't want to keep a strong reference of the self inside the block, you will need to use either [weak self] or [unowoned self] based on your use case.

Upvotes: 2

Shehata Gamal
Shehata Gamal

Reputation: 100541

You can try

self.beforeAdjustViews = { [weak self] (btnView) in 
 ///
}

Upvotes: 1

Related Questions