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 协议
基础教程推荐
- AdMob 广告未在模拟器中显示 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
