我正在将我的项目从 Objective-c 转换为 Swift,我正在使用一个 Swift 类,我有一个 protocol
我正在尝试在一个 Objective-c 类中访问.我的问题是,在objective-c 类中无法访问delegate
.这是我的快速课程:
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!
}
当我查看 MyProject-Swift.h
文件时,我没有看到 delegate
:
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
当我尝试在我的 Objective-c 类中使用 rateButtonView.delegate
时,出现编译器错误.
and when I try to use rateButtonView.delegate
in my Objective-c class, I get a compiler error.
有人知道这个问题吗?如何在 Objective-c 中访问 Swift 协议?
Anyone know the issue? How do access a Swift protocol in Objective-c?
您需要使用 @objc
属性标记您的协议,以便它可以从 Objective-C 访问:
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)
}
Apple 文档中的此页面 涵盖了该问题.
这篇关于在 Objective-C 中使用 Swift 协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!