ios - 使 swift 类符合协议(protocol) - 在静态/类级别

标签 ios swift protocols swift-protocols

我正在尝试在 Swift 中构建一个通用的 UITableViewController 子类,它将容纳任意数量的不同类型的表格 View 单元格,而无需了解其中任何一个的内部知识。

为此,我正在尝试为我的模型和我的表格 View 单元格使用协议(protocol)。模型的协议(protocol)将返回我应该去哪个单元格类,单元格的协议(protocol)将返回对给定模型的单元格高度应该是多少等问题的答案。

但是我在使协议(protocol)工作时遇到了问题,因为对于第二个协议(protocol),我想转到单元格的类而不是它的实例。

模型协议(protocol)如下:

protocol JBSTableItemDelegate
{
    func tableCellDelegate() -> JBSTableViewCellInterface
}

细胞的操作规程如下:

protocol JBSTableViewCellInterface: class
{
    static func registerNibsWithTableView(tableView: UITableView)

    static func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath?, tableItem: JBSTableItemDelegate) -> CGFloat

    static func tableView(tableView: UITableView, dequeueReusableCellWithIndexPath indexPath: NSIndexPath, tableItem: JBSTableItemDelegate, delegate: AnyObject) -> JBSTableViewCell
}

注意关键字“static”的使用。这些方法是 UITableViewCell 子类中的类方法,添加静态似乎是我需要做的来验证这些类是否符合,或者我理解。

当我使用第一个协议(protocol)时,代码看起来像这样,并且可以编译:

let tableViewCellInterface = tableItem!.tableViewCellInterface()

它正在调用此方法(作为一个示例):

func tableViewCellInterface() -> JBSTableViewCellInterface
{
    return JBSLiteratureTableViewCell.self as! JBSTableViewCellInterface
}

这将返回单元格的类,例如“JBSLiteratureTableViewCell.self”

当我使用第二个协议(protocol)时,代码看起来像这样,但无法编译:

returnFloat = tableViewCellInterface.tableView(tableView, heightForRowAtIndexPath: indexPath, tableItem: tableItem!)

由于前面的static关键字导致编译失败,我得到的编译错误是:

'JBSTableViewCellInterface' does not have a member named 'tableView'

如果我从协议(protocol)函数中删除静态关键字,它会编译,但随后 UITableViewCell 子类会提示说:

'JBSLiteratureTableViewCell' does not conform to protocol 'JBSTableViewCellInterface'

这是因为他们现在正试图确保实例方法存在,而这不是它们所在的位置。

如何使 swift 类在类级别符合协议(protocol),以便它可以成为我的委托(delegate)而不是类的某个实例?我确信我可以通过创建协议(protocol) JBSTableViewCellInterface 的辅助类来解决这个问题,它们是单例并让它们完成工作,但我宁愿将它直接构建到类方法中的 UITableViewCell 子类中。

最佳答案

已更新 Swift 2.0 及更高版本

根据 Gregzo 的回答,Swift 2.0+ 允许在协议(protocol)定义中将方法声明为静态方法。这些必须满足实现协议(protocol)的对象中的静态/类方法。

您无法使用静态方法满足实例方法的协议(protocol)定义,反之亦然,这使得上述问题的答案不完整。

如果你想尝试这个,只需在你的协议(protocol)定义中使用关键字“static”,你将在你的一致对象中实现为静态或类方法:

protocol InstanceVsStatic {
    func someInstanceFunc()
    static func someStaticFunc()
}

enum MyConformingEnum: InstanceVsStatic {
    case someCase

    static func someStaticFunc() {
           // code
    }
    func someInstanceFunc() {
        // code
    }
}

class MyConformingClass: InstanceVsStatic {
    class func someStaticFunc() {
           // code
    }
    func someInstanceFunc() {
        // code
    }
}

struct MyConformingStruct: InstanceVsStatic {
    static func someStaticFunc() {
           // code
    }
    func someInstanceFunc() {
        // code
    }
}

您可以让实例方法调用静态/类方法:

这允许您在需要遵守需要实例方法的协议(protocol)时执行静态代码。

struct MyConformingStruct: InstanceVsStatic {
    static func doStuffStatically(){
        // code
    }

    static func someStaticFunc() {
           // code
    }

    func someInstanceFunc() {
        MyConformingStruct.doStuffStatically()
    }
}

swift 1.2

除了上述间接方式之外,没有办法使用静态(类)方法来符合纯 swift 1.2 及以下版本中的协议(protocol)。这是一个已知错误/未实现的功能:https://openradar.appspot.com/20119848

关于ios - 使 swift 类符合协议(protocol) - 在静态/类级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29711935/

相关文章:

iphone - 触摸位置的值超出 View 范围

swift - 在多个 UICollectionViewCell 之间传递数据

iOS 当应用程序处于后台时处理通知和有效负载

xcode - Swift:枚举类型作为属性

swift - 如果 Swift 协议(protocol)是用类型约束定义的,为什么不能直接访问该类型的属性/方法?

swift - swift 协议(protocol)中的泛型

iphone - 如何从COCOS3D中的URL加载.pod文件

ios - UIViewControllerAnimatedTransitioning 与 UIStatusBarAnimation

ios - 如何通过导航 View Controller 推送数据

ios - UICollectionView 在滚动时卡住