How to weak self when setting function to closure as in 1. to work same as example 2. Any ideas?
class TabController: UITabBarController {
func setupViewActions() {
tabView.itemTapped = changeTab // 1. tabController cannot be deallocated
tabView.itemTapped = { [weak self] tab in // 2. TabController can be deallocated
changeTab(tab: tab)
}
}
func changeTab(tab: TabItem) {
self.selectedIndex = tab.index
}
}
enum TabItem {
var index: int
}
class TabView: UIView {
var itemTapped: ((_ tab: TabItem) -> Void)?
}
I really don't see any problem with
In my opinion, this is pretty common pattern in Swift and, especially, Cocoa.
However, if you really want to get rid of this curly braces, you can use this wrapper function using parameter packs:
Example usage:
Code above prints