特定类的 Swift 协议(protocol)?

标签 swift

我正在构建一个 UIPresentationController 子类。 UIPresentationController 定义了一个默认初始化器,如下所示:

init(presentedViewController: UIViewController , presentingViewController: UIViewController?)

现在,为了让它工作,我希望我的 presentedViewController 符合某些协议(protocol),比如 MyRandomProtocol

我如何重写我的初始化程序,使其第一个参数既是 UIViewController 子类,又是一个特别符合 MyRandomProtocol 的子类?

最佳答案

您可以使用泛型。

init<T: UIViewController>(presentedViewController: T, ...) where T: MyRandomProtocol  {
    //initialization code
}

关于特定类的 Swift 协议(protocol)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41918441/

相关文章:

swift - 设备上的 WebView 目录访问被拒绝

ios - Swift 中的错误检测终端引用变量作为问题 - 变量不存在

iphone - 使用 Swift 的 FMDatabaseQueue (FMDB)

ios - 将 UInt64 乘以 float

ios - 如何使用 ScrollView 制作可滚动标签?

xcode - 升级到 Xcode 7 并得到这个 : Library not loaded: @rpath/libswiftCore. dylib

ios - 如何将自定义数据添加到标记(Google Maps API SWIFT)

ios - 没有让 Core Motion 在 UI 中显示加速度计数据

ios - swift animateWithDuration 动画不正确

arrays - 在 UITableView iOS Swift 中循环遍历数组并使用自定义单元格设置值时出现问题