swift 3 - 方法不覆盖其超类中的任何方法
swift 3 - method does not override any methods from its superclass
我正在尝试覆盖 Swift 子类中的 myMethod,但不断出现以下错误:
method does not override any methods from its superclass
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")
}
}
有人能告诉我我错过了什么吗?
Swift 只会看到您在桥接 header 中包含的 header 文件 – 因此如果您不在 @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
我正在尝试覆盖 Swift 子类中的 myMethod,但不断出现以下错误:
method does not override any methods from its superclass
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")
}
}
有人能告诉我我错过了什么吗?
Swift 只会看到您在桥接 header 中包含的 header 文件 – 因此如果您不在 @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