7

I'm working on converting my project from Objective-c to Swift, and a Swift class I'm using, I have a protocol I'm trying to access in an Objective-c class. My problem is, the delegate is not accessible in the objective-c class. Here is my swift class:

protocol RateButtonDelegate {
    func rateButtonPressed(rating: Int)
}

class RateButtonView: UIView {


    var delegate: RateButtonDelegate?

    var divider1: UIView!
    var divider2: UIView!
}

When I look at the MyProject-Swift.h file, I don't 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.

Anyone know the issue? How do access a Swift protocol in Objective-c?

coder
  • 10,460
  • 17
  • 72
  • 125

1 Answers1

6

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

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

This page from Apple's documentation covers the issue.

Nate Cook
  • 92,417
  • 32
  • 217
  • 178