@property (strong, nonatomic) UIViewController<UITableViewDelegate> *thing;
我希望在Swift中实现类似于这个Objective-C代码中的属性。这是我尝试过的代码:
class AClass<T: UIViewController where T: UITableViewDelegate>: UIViewController {
var thing: T!
}
这个可以编译。当我从storyboard添加属性时出现问题。 @IBOutlet
标签会生成编译器错误。
class AClass<T: UIViewController where T: UITableViewDelegate>: UIViewController {
@IBOutlet weak var anotherThing: UILabel! // error
var thing: T!
}
错误:
Variable in a generic class cannot be represented in Objective-C
我实现这个正确吗?我该怎么修复或避开这个错误?
编辑:
Swift 4终于有了解决这个问题的方法。请查看我的更新答案。