Using a Swift protocol in Objective-C(在 Objective-C 中使用 Swift 协议)
问题描述
我正在将我的项目从 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 协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Objective-C 中使用 Swift 协议
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- URL编码Swift iOS 2022-01-01
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- SetOnItemSelectedListener上的微调程序错误 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- UITextView 内容插图 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- GPS状态的广播接收器? 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01
