子类对象未在swift中显示父类变量

时间:2017-09-17 21:59:53

标签: ios objective-c swift swift-protocols

我正面临这个奇怪的问题,不知道为什么。 我有一个类MPI_Comm_free(),它有一个协议的委托变量:

View

现在这个class View: UIView { weak var delegate : SampleProtocol? } protocol SampleProtocol: Class { } 类是另一个类的父类:

View

当我在Objective-C类中创建class AnotherView : View IBOutlet时,它无法访问AnotherView变量,也无法在swift.h文件中看到。

有人可以解释我在这里做错了吗?

1 个答案:

答案 0 :(得分:3)

您需要定义您的协议,Objective-C可以添加@objc,正如我在评论中所说的那样

@objc protocol SampleProtocol: class { }

之后,您必须在 .m

中添加此行
#import "YourProjectName-Swift.h"

完整代码

import UIKit

@objc protocol SampleProtocol: class { }

class View: UIView {
    weak var delegate : SampleProtocol?
}

class AnotherView : View{

}

遗产部分没有副作用,经过测试

现在您可以访问View课程的委托属性而不会出现任何问题,如图所示

enter image description here