Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使swift等级符合协议-在静态/等级级别_Ios_Swift_Protocols_Swift Protocols - Fatal编程技术网

Ios 使swift等级符合协议-在静态/等级级别

Ios 使swift等级符合协议-在静态/等级级别,ios,swift,protocols,swift-protocols,Ios,Swift,Protocols,Swift Protocols,我正在尝试在Swift中构建一个通用的UITableViewController子类,该子类可以容纳任意数量的不同类型的表视图单元格,而不需要了解它们中的任何一个 为了做到这一点,我尝试对模型和表视图单元格使用协议。模型的协议将返回我应该使用的单元类,单元的协议将返回给定模型的单元高度等问题的答案 但是我在使协议工作时遇到了一个问题,因为对于第二个协议,我希望转到cell的类,而不是它的实例 模型的协议如下所示: protocol JBSTableItemDelegate { func

我正在尝试在Swift中构建一个通用的UITableViewController子类,该子类可以容纳任意数量的不同类型的表视图单元格,而不需要了解它们中的任何一个

为了做到这一点,我尝试对模型和表视图单元格使用协议。模型的协议将返回我应该使用的单元类,单元的协议将返回给定模型的单元高度等问题的答案

但是我在使协议工作时遇到了一个问题,因为对于第二个协议,我希望转到cell的类,而不是它的实例

模型的协议如下所示:

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
}
这些单元的协议如下所示:

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

当我使用第一个协议时,代码看起来是这样的,它编译:

let tableViewCellInterface = tableItem!.tableViewCellInterface()
它调用此方法(作为一个示例):

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

当我使用第二个协议时,代码看起来是这样的,并且它不会编译:

returnFloat = tableViewCellInterface.tableView(tableView, heightForRowAtIndexPath: indexPath, tableItem: tableItem!)
由于前面的static关键字up,它无法编译,我得到的编译器错误是:

“JBSTableViewCellInterface”没有名为“tableView”的成员

如果我从protocol funcs中去掉静态关键字,它会编译,但是UITableViewCell子类会抱怨说:

“JBSLiteratureTableViewCell”不符合协议“JBStableViewCell”

这是因为他们现在正试图确保实例方法存在,而这些实例方法并不存在


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

如果协议函数是静态的,实现者应该将其作为静态方法来实现,如果不是,作为实例方法:

protocol MixedProtocol
{
    static func staticFoo()
    func instanceBar()
}

class ExampleClass : MixedProtocol
{
    // implementing static func as class func is fine.
    // class funcs are overridable, not static ones
    class func staticFoo()
    {
        println( "I'm a class func" )
    }

    func instanceBar()
    {
        println( "I'm an instance func" )
    }
}

没有捷径可走:遵守协议就意味着这一点,“静态”是协议成员声明的关键特性,实施者必须遵守。

针对Swift 2.0及以上版本更新

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

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

如果您想尝试此方法,只需在协议定义中使用关键字“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
    }
}
您可以让实例方法调用静态/类方法:

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

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

    static func someStaticFunc() {
           // code
    }

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


除上述间接方式外,在纯swift版本1.2及以下版本中,无法使用静态(类)方法来遵守协议。这是一个已知的错误/未实现的功能:

真的吗?你有链接吗?想知道是否有可能在将来会有一个功能?