Implement objectivec protocol in fast

I am trying to implement this optional protocol method from Objective-C to swift:

- (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol logWithFormat: (NSString *)format arguments:(va_list)arguments; 

(cfr: https://developer.apple.com/library/ios/samplecode/CustomHTTPProtocol/Introduction/Intro.html ) I quickly wrote this method:

 func customHTTPProtocol(`protocol`: CustomHTTPProtocol!, logWithFormat format: String!, arguments: CVaListPointer) { } 

he complains that he cannot satisfy the optional requirement and suggests adding the @objc before method, but if I add @objc, he will throw an error (CVaListPointer cannot be represented in Objective-C)

The problem is that this test fails:

 if ([strongDelegate respondsToSelector:@selector(customHTTPProtocol:logWithFormat:arguments:)]) { 

and the quick method is not called

+5
source share
1 answer

If you want to use objective-c @protocol in the swift class, then you import the objective-c class into your Bridging-Header file, which Xcode creates when using the objective-c file in your fast project. Then, obviously, you need to add a delegate to the fast file where you need to use it.

 class classname : baseClass<yourDelegate> { } 

In this file, you first need to add all the necessary delegation methods, and then add an additional method that you must use. If you do not add the required delegate method, it will give you an error.

However, if you want to use protocol from swift to objective-c, you need to add @objc in front of the protocol name and import the swift file into objective-c file, you will also add @objc in front of the class, for example this,

 @objc protocol DelegateName { //declare your required and optional delegate method here } @objc classname : baseClass<yourDelegate> { } 

And then import your quick class into your objective-c file, e.g.

 #import <PROJ_NAME/PROJ_DIR-Swift.h> 

And it’s important to add:

 classObj.delegate = self 
0
source

Source: https://habr.com/ru/post/1236119/


All Articles