使swift类符合协议 - 在静态/类级别 [英] Make swift class conform to protocol - at static/class level

查看:137
本文介绍了使swift类符合协议 - 在静态/类级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Swift中构建一个通用的UITableViewController子类,它可以容纳任意数量的不同类型的表视图单元,而不具备其中任何一个的内部知识。

I'm trying to build a generic UITableViewController subclass in Swift that will accommodate any number of different kinds of table view cells without having internal knowledge of any of them.

要做到这一点,我正在尝试为我的模型和我的表视图单元格使用协议。模型的协议将返回我应该去的哪个单元格类,并且单元格的协议将返回问题的答案,例如给定模型的单元格高度应该是什么。

To do this I'm trying to use protocols for my models and for my table view cells. The protocol for the models will return which cell class I should be going to, and the protocol for the cells will return answers to questions like what the height of the cell should be for a given model.

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

But I'm having a problem making the protocols work, because with the second protocol I want to go to the cell's class rather than its instance.

模型的协议如下:

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子类中的类方法,添加静态似乎是我需要做的,以验证这些类是否符合,或者我理解。

Notice the use of the keyword "static". These methods are class methods in the UITableViewCell subclasses, and adding static seems to be what I need to do to verify that these classes conform, or so I understand.

当我使用代码看起来如此的第一个协议,它编译:

When I use the first protocol the code looks like so, and it compiles:

let tableViewCellInterface = tableItem!.tableViewCellInterface()

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

It's calling this method (as one example):

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

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

This returns the class of the cell, such as, "JBSLiteratureTableViewCell.self"

当我使用第二个协议时代码看起来如此,并且它不能编译:

When I use the second protocol the code looks like so, and it does not compile:

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

由于之前的static关键字,它无法编译,我得到的编译器错误是:

It fails to compile because of the static keyword up earlier, and the compiler error I get is:


'JBSTableViewCellInterface'确实没有一个名为'tableView'的成员

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

如果我从协议功能中取出静态关键字,它会编译,但随后是UITableViewCell子类抱怨说:

If I take the static keywords off of the protocol funcs, it compiles, but then the UITableViewCell subclasses complain saying:


'JBSLiteratureTableViewCell'不符合协议'JBSTableViewCellInterface'

'JBSLiteratureTableViewCell' does not conform to protocol 'JBSTableViewCellInterface'

这是因为他们现在正在尝试g以确保实例方法存在,而不是这些方法。

This is because they're now trying to make sure instance methods exist, which is not where these are.

如何使swift类符合类级别的协议,因此它可以是我的代表而不是某个类的实例?我确信我可以通过创建单独的协议JBSTableViewCellInterface的辅助类来解决这个问题,并让他们完成工作,但我宁愿在它们的类方法中将它构建到UITableViewCell子类中。

How do I make a swift class conform to a protocol at the class level, so it can be my delegate rather than some instance of a class? I'm sure I could get around this by creating helper classes of protocol JBSTableViewCellInterface that are singletons and let them do the work, but I'd rather build it right into the UITableViewCell subclasses in their class methods.

推荐答案

为Swift 2.0及以上版本更新

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

As per Gregzo's answer, Swift 2.0+ allows methods to be declared as static in the protocol definition. These must be satisfied with static/class methods in objects that implement the protocol.

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

You cannot satisfy a protocol definition for a instance method with a static method or vice-versa, which makes this an incomplete answer for the question above.

如果你想尝试这个,只需在你的协议定义中使用关键字static来实现你将作为静态实现的方法或符合对象中的类方法:

If you want to try this just use the keyword "static" in your protocol definition for methods you will implement as static or class methods in your conforming objects:

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
    }
}

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

这允许您在需要符合a时执行静态代码需要实例方法的协议。

This allows you to execute static code when you need to conform to a protocol that requires an instance method.

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

    static func someStaticFunc() {
           // code
    }

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

Swift 1.2

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

Other than indirectly as above, there is no way to use static (class) methods to conform to a protocol in pure swift version 1.2 and below. It is a known bug / unimplemented feature: https://openradar.appspot.com/20119848

这篇关于使swift类符合协议 - 在静态/类级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆