Ios 在swift中实现objectivec协议
我试图在swift中从Objective-C实现此可选协议方法:Ios 在swift中实现objectivec协议,ios,objective-c,swift,Ios,Objective C,Swift,我试图在swift中从Objective-C实现此可选协议方法: - (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol logWithFormat: (NSString *)format arguments:(va_list)arguments; func customHTTPProtocol(`protocol`: CustomHTTPProtocol!, logWithFormat format: String!, argume
- (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol logWithFormat:
(NSString *)format arguments:(va_list)arguments;
func customHTTPProtocol(`protocol`: CustomHTTPProtocol!, logWithFormat format: String!, arguments: CVaListPointer) {
}
(cfr:)
我已在swift中编写了此方法:
- (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol logWithFormat:
(NSString *)format arguments:(va_list)arguments;
func customHTTPProtocol(`protocol`: CustomHTTPProtocol!, logWithFormat format: String!, arguments: CVaListPointer) {
}
它抱怨无法满足可选要求,并建议在方法之前添加@objc,但如果我添加@objc,它会给出一个错误(CVaListPointer不能在Objective-C中表示)
问题在于该测试失败:
if ([strongDelegate respondsToSelector:@selector(customHTTPProtocol:logWithFormat:arguments:)]) {
而且swift方法不被调用如果您想在swift类中使用objective-c
@protocol
,那么您必须将objective-c类导入到桥接头文件中,该桥接头文件是在swift项目中使用objective-c文件时由XCode创建的。显然,您必须在swift文件中添加代理,在该文件中,您必须像这样使用它
class classname : baseClass<yourDelegate> {
}
然后像这样将swift类导入objective-c文件
@objc protocol DelegateName {
//declare your required and optional delegate method here
}
@objc classname : baseClass<yourDelegate> {
}
#import <PROJ_NAME/PROJ_DIR-Swift.h>
这已经完成了,我还有另一个问题,因为协议方法是可选的,并且(va_list)可能翻译错误