【问题标题】:Using a Swift protocol in Objective-C在 Objective-C 中使用 Swift 协议
【发布时间】:2014-12-15 19:18:08
【问题描述】:

我正在努力将我的项目从 Objective-c 转换为 Swift,并且我正在使用一个 Swift 类,我有一个 protocol 我正在尝试在一个 Objective-c 类中访问。我的问题是,在objective-c 类中无法访问delegate。这是我的快速课程:

protocol RateButtonDelegate {
    func rateButtonPressed(rating: Int)
}

class RateButtonView: UIView {


    var delegate: RateButtonDelegate?

    var divider1: UIView!
    var divider2: UIView!
}

当我查看MyProject-Swift.h 文件时,我没有看到delegate

@interface RateButtonViewTest : UIView
@property (nonatomic) UIView * divider1;
@property (nonatomic) UIView * divider2;
@end

当我尝试在我的 Objective-c 类中使用 rateButtonView.delegate 时,出现编译器错误。

有人知道这个问题吗?如何在 Objective-c 中访问 Swift 协议?

【问题讨论】:

    标签: objective-c swift delegates


    【解决方案1】:

    您需要使用@objc 属性标记您的协议,以便它可以从Objective-C 访问:

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

    This page from Apple's documentation 涵盖了这个问题。

    【讨论】:

    • 你是一个救生员。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2015-01-01
    • 2016-07-06
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多