【发布时间】:2017-07-06 13:54:29
【问题描述】:
我想使用一个带有 SwipeTableViewCell 类的库(从 UITableViewCell 派生),但它只支持 iOS 9,所以我想尽可能从该类派生,但如果应用程序在 9.0 以下运行,我想从普通 UITableViewCell 类派生.
这是我能想到的最好的(不工作):
@available(iOS, obsoleted: 9.0)
class MyTableViewCell : UITableViewCell {
}
@available(iOS 9.0, *)
class MyTableViewCell : SwipeTableViewCell {
}
然后我尝试从那个类派生:
class SomeOtherTableViewCell : MyTableViewCell {
}
Xcode 给了我以下错误并链接到上面的两个类定义:
SomeOtherTableViewCell.swift:34:31: 'MyTableViewCell' is ambiguous for type lookup in this context
Found this candidate
Found this candidate
在 Swift 中根据 API 级别有条件地从两个不同的类派生的酷方法是什么?
【问题讨论】:
-
继承是在编译时评估的,而不是在运行时。
-
@MartinR 这很糟糕,所以你说这根本不可能?
-
也许我忽略了一些东西,但我假设您必须定义两个子类 MyTableViewCell 和 MySwipeTableViewCell,然后在运行时使用
if #available()创建其中一个或另一个的实例。 -
是的,作为最后的手段,但我们想将它用作许多不同 TableViewCells 的基类,所以它会重复很多代码。
标签: swift inheritance swift3