I was a few days ago from Objective-C to write Swift language, in the project I have encountered a problem.This problem is when using respondsToSelector ("testEnum:") function to check whether to implement the function of the testEnum:,if the param is the case, it will return false, I have tried other types, it will return true, do not know what is the reason, see the following code, to help me solve it, thank you very much!
enum TestEnum {
case A
case B
case C
}
protocol TestAProtocol: NSObjectProtocol {
func testEnum(testEnum: TestEnum);
func testInt(testInt: Int);
}
class TestA: NSObject {
var delegate: TestAProtocol?;
func executeDelegateCallBack() {
if (delegate != nil && delegate!.respondsToSelector(Selector("testEnum:"))) { // delegate!.respondsToSelector(Selector("testEnum:")) return false ?
delegate?.testEnum(TestEnum.A);
}
if (delegate != nil && delegate!.respondsToSelector(Selector("testInt:"))) { // delegate!.respondsToSelector(Selector("testInt:")) return true ?
delegate?.testInt(0);
}
}
}
class TestB: NSObject, TestAProtocol {
func initTestB () {
let testA: TestA = TestA();
testA.delegate = self;
testA.executeDelegateCallBack();
}
// mark TestAProtocol
func testInt(testInt: Int) {
}
func testEnum(testEnum: TestEnum) {
}
}
respondsToSelector()uses the Objective-C runtime and works only with methods which are Objective-C compatible. Swiftenumscan only be represented in Objective-C if they are marked with@objc, and that requires that they have an integer raw value.So with
your
respondsToSelector(Selector("testEnum:")will returntrue.Note however that testing for the presence of a method makes only sense with optional protocol methods, and these are only available for
@objcprotocols, for example:And then it is much simpler to use optional chaining instead of
respondsToSelector:or more detailed: