如何同时使用带有可选和扩展的@objc协议?

时间:2016-07-04 18:48:38

标签: ios objective-c swift protocols swift-extensions

这段代码没有编译,可能听起来很愚蠢,但我会解释为什么它如此重要!

@objc protocol p {
    optional func f1()
    func f2()
}

extension p {
    func f1() { }
    func f2() { }
}


class foo: p {
}

编译器说Type c does not conform to protocol 'p',这可能是因为你不能同时使用@objc可选和扩展(并且在这种情况下也没有出现)。但请考虑以下示例:

我想在我的扩展中的协议中定义的非可选方法上设置一个选择器(我使用@objc的主要原因):

func f1() { } - > func f1() { ... #selector(Self.f2) ... }

我还希望我的f2()函数具有默认行为。如果我将f2()标记为optional,则无法在#selector中使用它,因为编译器在需要时不知道此方法是否实际存在。当然有很多讨厌的解决方法,比如全局方法,将Selector作为输入发送给方法等,但有没有一种干净的方法来实现它?

这是实际问题

@objc
protocol Refreshable {
    weak var refreshControl: UIRefreshControl? { get set }
    optional func setupRefreshControl()
    func refresh()
}

@objc
protocol ContentLoader {
    func load(reset: Bool)
}

extension Refreshable where Self: ContentLoader {
    func refresh() {
        delay(0.75) { [weak self] in
            self?.load(true)
        }
    }
}

extension Refreshable where Self: UICollectionViewController {
    func setupRefreshControl() {
        let newRefreshControl = UIRefreshControl()

        newRefreshControl.tintColor = UIColor.grayColor()
        newRefreshControl.addTarget(self, action: #selector(Self.refresh), forControlEvents: .ValueChanged)
        collectionView?.addSubview(newRefreshControl)
        refreshControl = newRefreshControl
    }
}

现在,如果ViewController实现RefreshableContentLoader,它就找不到默认的refresh函数,但确实找到setupRefreshControl。所以我想让我们将refresh标记为可选,但通过这样做,你不能再将它发送给选择器了。

我甚至试过这个:

func refresh() - > optional func refresh()

let str = "refresh"
let sel = Selector(str)

它使编译器静默,但不起作用......上升unrecognized selector sent to instance....

1 个答案:

答案 0 :(得分:1)

我认为这在swift中是不可能的(因为它与@objc协议的桥接方式)。但这是一个解决unrecognized selector sent to instance...问题的工作(使用Obj-c关联对象)。

fileprivate class AssociatedObject: NSObject {
    var closure: (() -> ())? = nil

    func trigger() {
        closure?()
    }
}

// Keys should be global variables, do not use, static variables inside classes or structs.
private var associatedObjectKey = "storedObject"

protocol CustomProtocol: class {
    func setup()
}

extension CustomProtocol where Self: NSObject {

    fileprivate var associatedObject: AssociatedObject? {
        get {
            return objc_getAssociatedObject(self, &associatedObjectKey) as? AssociatedObject
        }

        set {
            objc_setAssociatedObject(self, &associatedObjectKey, newValue, .OBJC_ASSOCIATION_RETAIN)
        }
    }


    func setup() {
        let object = AssociatedObject()
        object.closure = { [weak self] in // Do not forget to use weak in order to avoid retain-cycle
            self?.functionToCallIndirectlyWithSelector()
        }

        let selector = #selector(object.trigger)
//      Uncomment next line to test it's functionality
        object.perform(selector)

//      Here, you must add selector to the target which needs to call the selector, for example:
//      refreshControl.addTarget(object, action: selector, forControlEvents: .valueChanged)

        self.associatedObject = object
    }

    func functionToCallIndirectlyWithSelector() {
        print("Function got called indirectly.")
    }
}


class CustomClass: NSObject, CustomProtocol {}

let instance = CustomClass()

instance.setup()

我添加了Self: NSObject约束,以便能够在游乐场测试它的功能,我不确定它是否有必要。