# A shortcut for weak actions

**URL:** <https://forums.swift.org/t/a-shortcut-for-weak-actions/11791>\
**Category:** Using Swift\
**Created:** [April 9, 2018, 4:41pm UTC](https://forums.swift.org/t/a-shortcut-for-weak-actions/11791 "2018-04-09T16:41:21Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![zoul](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/zoul/32/9559_2.png) [@zoul](https://forums.swift.org/u/zoul)\
**Post date:** [April 9, 2018, 4:41pm UTC](https://forums.swift.org/t/a-shortcut-for-weak-actions/11791/1 "2018-04-09T16:41:21Z")

</div>

A have a sort of coordinator class that builds several `UIViewController`s and sets some closures to customize their behaviour:

```swift
func termsAndConditions() -> UIViewController {
    let vc = TermsConditionsController()
    vc.completion = { [weak self] in
        guard let `self` = self else { return }
        self.navigationVC.push(self.welcomeScreen(), animated: true)
    }
    return vc
}

func welcomeScreen() -> UIViewController {
    let vc = WelcomeScreenController()
    vc.signUpAction = { [weak self] in
        guard let `self` = self else { return }
        self.navigationVC.push(self.userProfileScreen(), animated: true)
    }
    return vc
}

```

How can I get rid of the repeated weak-callback code? I tried to toy with [unapplied methods](https://forums.swift.org/t/introduce-flattened-unapplied-method-references-se-0042-revisited/11765), a bit like this:

```swift
typealias Action = () -> Void
typealias Screen = (Self) -> () -> UIViewController

func push(_ screen: @escaping Screen, animated: Bool = true) -> Action {
    return { [weak self] in
        guard let `self` = self else { return }
        self.navigationVC.push(screen(self)(), animated: animated)
    }
}

```

But the unapplied method types come out a bit awkward and referring to methods in the same class requires qualifying with the current class name:

```swift
func welcomeScreen() -> UIViewController {
    let vc = WelcomeScreenController()
    vc.signUpAction = push(ThisClassName.userProfileScreen)
    return vc
}

```

I also tried toying with key paths (where the syntax ends up really nice, like `push(\.userProfileScreen)`), but then I am out of luck when the builder method needs an argument (`userProfileScreen(forUser: …)`).

Are there better options?
