Subclass 对象未在 swift 中显示父 class 变量
Subclass object not showing parent class variable in swift
我遇到了这个奇怪的问题,不知道为什么。
我有一个 class View
它有一个协议的委托变量:
class View: UIView {
weak var delegate : SampleProtocol?
}
protocol SampleProtocol: Class { }
现在这个 View
class 是另一个 class 的父 class:
class AnotherView : View
当我在 Objective-C class 中创建 AnotherView
的 IBOutlet
时,它无法访问 delegate
变量并且在swift.h 文件。
有人可以解释一下我做错了什么吗?
您需要通过 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
class 的委托 属性,如图所示
我遇到了这个奇怪的问题,不知道为什么。
我有一个 class View
它有一个协议的委托变量:
class View: UIView {
weak var delegate : SampleProtocol?
}
protocol SampleProtocol: Class { }
现在这个 View
class 是另一个 class 的父 class:
class AnotherView : View
当我在 Objective-C class 中创建 AnotherView
的 IBOutlet
时,它无法访问 delegate
变量并且在swift.h 文件。
有人可以解释一下我做错了什么吗?
您需要通过 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
class 的委托 属性,如图所示