How to suppress selector warning in Swift

Issue #777

Sometimes we need to use dynamic selector and that triggers warning in Swift

1
Selector("updateWithCount:") // Use '#selector' instead of explicitly constructing a 'Selector'

In ObjC we can use clang macro to suppress, like below

1
2
3
4
5
6
7
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-implementations"

- (void) deprecated_objc_method_override {
}

#pragma clang diagnostic pop

But in Swift, we can just use a dummy NSObject that has the needed methods, like

1
2
3
final class Dummy: NSObject {
@objc func update(count: Int) {}
}

#selector is just a safer way to construct Selector, they all yield same result as String

1
2
Selector("updateWithCount:") // updateWithCount:
#selector(Dummy.update(count:)) // updateWithCount:

Updated at 2021-02-18 10:11:13

Comments