Using the Swift Protocol in Objective-C

I am working on converting my project from Objective-c to Swift, and from the Swift class that I use, I have a protocol that I am trying to get in the Objective-c class. My problem is that delegate not available in the Objective-c class. Here is my quick class:

 protocol RateButtonDelegate { func rateButtonPressed(rating: Int) } class RateButtonView: UIView { var delegate: RateButtonDelegate? var divider1: UIView! var divider2: UIView! } 

When I look at the file MyProject-Swift.h , I do not see the delegate :

 @interface RateButtonViewTest : UIView @property (nonatomic) UIView * divider1; @property (nonatomic) UIView * divider2; @end 

and when I try to use rateButtonView.delegate in my Objective-c class, I get a compiler error.

Does anyone know a problem? How to access Swift protocol in Objective-c?

+5
source share
1 answer

You need to mark your protocol with the @objc attribute @objc that it is accessible from Objective-C:

 @objc protocol RateButtonDelegate { func rateButtonPressed(rating: Int) } 

This Apple documentation page describes this issue.

+6
source

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


All Articles