Using a Swift protocol in Objective-C

2019-01-15 15:18发布

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?

1条回答
该账号已被封号
2楼-- · 2019-01-15 16:04

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.

查看更多
登录 后发表回答