将关联类型限制为someClass

时间:2018-08-01 08:24:32

标签: swift associated-types

如何将我的关联类型限制为UITableViewUICollectionView

1 个答案:

答案 0 :(得分:1)

您可以很轻松地约束associatedtype:

public protocol MJRfreshAble {
    associatedtype RefreshableViewType where RefreshableViewType: UIScrollView

    var refreshableView: RefreshableViewType { get }
}

class TestVC1: UIViewController, MJRfreshAble {
    typealias RefreshableViewType = UITableView

    let tableView = UITableView()

    var refreshableView: RefreshableViewType {
        return tableView
    }
}

class TestVC2: UIViewController, MJRfreshAble {
    typealias RefreshableViewType = UICollectionView

    let collectionView = UICollectionView()

    var refreshableView: RefreshableViewType {
        return collectionView
    }
}
相关问题