swift 3 - 方法不会覆盖其超类中的任何方法

时间:2017-03-08 16:30:50

标签: objective-c swift inheritance override subclass

我正在尝试覆盖Swift子类中的 myMethod ,但一直收到以下错误:

  

方法不会覆盖其超类

中的任何方法

ViewControllerA.h

@interface ViewControllerA : UIViewController 
   // define a bunch of properties and methods
   // note: myMethod is NOT included in this interface
@end

ViewControllerA.m

@implementation ViewControllerA
    (void)myMethod:(ParameterClass *)parameter {
        ...
    }
@end

ViewControllerB.h

@interface ViewControllerB : ViewControllerA
   // define a bunch of properties
@end

ViewControllerB.m

@implementation ViewControllerB
    // define a bunch of methods
@end

ViewControllerC.swift

class ViewControllerC: ViewControllerB{
    override func myMethod(parameter: ParameterClass) { // ERROR is here
        NSLog("Calling overrided myMethod")
    }
}

有人可以告诉我我错过了什么吗?

1 个答案:

答案 0 :(得分:2)

Swift只能看到你在桥接标题中包含的头文件 - 所以如果你没有在@interface ViewControllerA中包含方法声明,那么Swift就无法知道任何关于它

所以,只需将其放入@interface

@interface ViewControllerA : UIViewController

// By default it will be imported into Swift as myMethod(_:). Adding NS_SWIFT_NAME
// allows you to change that to whatever you want.
-(void)myMethod:(ParameterClass *)parameter NS_SWIFT_NAME(myMethod(parameter:));

@end