Swift3 有没有可能有一个Swift协议来强制静态方法而不是类方法,反之亦然?

Swift3 有没有可能有一个Swift协议来强制静态方法而不是类方法,反之亦然?,swift3,protocols,Swift3,Protocols,有没有可能有一个Swift协议来强制静态方法而不是类方法,反之亦然 例如,即使协议设置为类协议,也不允许使用类func或最终类func: protocol MyProtocol: class { final class func dummyClassMethod() } 或者在这种情况下,当符合以下条件时,允许类创建静态方法或类方法: protocol MyProtocol: class { static func dummyClassMethod() } 你不能这样做,因为

有没有可能有一个Swift协议来强制静态方法而不是类方法,反之亦然

例如,即使协议设置为类协议,也不允许使用类func或最终类func:

protocol MyProtocol: class {
    final class func dummyClassMethod()
}
或者在这种情况下,当符合以下条件时,允许类创建静态方法或类方法:

protocol MyProtocol: class {
    static func dummyClassMethod()
}

你不能这样做,因为苹果的文档明确表示,为了这个目的,只能使用
static

在协议中声明
静态
方法要求 声明,
静态
声明标记方法声明
修改器

资料来源:



当您在
中实现
协议
静态
方法时,在实现中使用
静态
没有区别

protocol ProtocolForClasses: class {
  static func method()
}

class ClassOne: ProtocolForClasses {
  class func method() {

  }
}

class ClassTwo: ProtocolForClasses {
  static func method() {

  }
}

“在实现中使用
静态
没有区别”→ 实际上有一个区别,
静态
方法不能在子类中重写,而
方法可以重写。