协议扩展中的Swift闭包

时间:2016-06-28 09:26:20

标签: ios swift protocols

我希望装饰UIViewController能够在从另一个类(例如Network State Manager)调用setInteractionEnabled方法时调整它的界面。应通过覆盖onInteractionChanged在具体控制器中提供所有更改(如果有)。这是我的代码:

import Foundation

typealias InteractionClosure = ((enabled: Bool) -> Void)

protocol Interaction: class {

    var onInteractionChanged: InteractionClosure? { get set }

    func setInteractionEnabled(enabled: Bool)

}

extension Interaction where Self: UIViewController {

    // Default: Do nothing
    // Throws: - Extensions may not contain stored properties
    var onInteractionChanged: InteractionClosure? = nil

    func setInteractionEnabled(enabled: Bool) {
        onInteractionChanged?(enabled: enabled)
    }

}

extension UIViewController : Interaction {}

如何为onInteractionChanged添加默认实施?

2 个答案:

答案 0 :(得分:4)

回答我自己的问题通常是我不做的事,但这是我的解决方案:

typealias InteractionClosure = (enabled: Bool) -> Void

protocol Interaction: class {

    func addOnInteractionChanged(closure: InteractionClosure)
    func setInteractionEnabled(enabled: Bool)

}

extension Interaction where Self: UIViewController {

    func addOnInteractionChanged(closure: InteractionClosure) {
        onInteractionChanged = closure
    }

    func setInteractionEnabled(enabled: Bool) {
        onInteractionChanged?(enabled: enabled)
    }

    // MARK: - Private

    private var onInteractionChanged: InteractionClosure? {
        get {
            let wrapper =
                objc_getAssociatedObject(self, &icAssociationKey) as? ClosureWrapper
            return wrapper?.closure
        }
        set(newValue) {
            objc_setAssociatedObject(self,
                                     &icAssociationKey,
                                     ClosureWrapper(newValue),
                                     .OBJC_ASSOCIATION_RETAIN)
        }
    }

}

extension UIViewController : Interaction {}

// Helpers

private var icAssociationKey: UInt8 = 0

private class ClosureWrapper {
    var closure: InteractionClosure?

    init(_ closure: InteractionClosure?) {
        self.closure = closure
    }
}

客户端类:

class LoginViewController: UIViewController {

    // MARK: - Lifecycle

    override func viewDidLoad() {
        super.viewDidLoad()
        self.setup()
    }

    // MARK: - Private

    private func setup() {
        // ...

        addOnInteractionChanged { [unowned self] (enabled) in            
            self.signInButton.enabled = enabled
            self.activityIndicatorView.hidden = !enabled
        }
    }

}

在经理班:

visibleViewController?.setInteractionEnabled(true)

答案 1 :(得分:0)

如果您希望财产仅具有{ get }能力,您可以使用:

protocol TestProtocol {
    var testClosure: ((_ parameter: Bool) -> Void)? { get }
}

extension TestProtocol {
    var testClosure: ((_ parameter: Bool) -> Void)? {
        return { parameter in
            print(parameter)
        }
    }
}