mas/MasKitTests/Strongify.swift

27 lines
745 B
Swift
Raw Normal View History

2019-01-08 22:43:33 -07:00
//
// Strongify.swift
// MasKitTests
//
// Created by Ben Chatelain on 1/8/19.
// Copyright © 2019 mas-cli. All rights reserved.
//
// https://medium.com/@merowing_/stop-weak-strong-dance-in-swift-3aec6d3563d4
2021-03-21 22:25:18 -07:00
func strongify<Context: AnyObject, Arguments>(
_ context: Context?,
closure: @escaping (Context, Arguments) -> Void
) -> (Arguments) -> Void {
2021-03-21 22:46:17 -07:00
let strongified = { [weak context] (arguments: Arguments) in
2019-01-08 22:43:33 -07:00
guard let strongContext = context else { return }
closure(strongContext, arguments)
}
2021-03-21 22:46:17 -07:00
return strongified
2019-01-08 22:43:33 -07:00
}
func strongify<Context: AnyObject>(_ context: Context?, closure: @escaping (Context) -> Void) {
guard let strongContext = context else { return }
closure(strongContext)
}