简体   繁体   English

Swift在Protocol中声明了Class Func

[英]Swift Declare Class Func in Protocol

I had following confusion. 我有以下困惑。 As far as I know the main difference between static and class keywords when declaring method is that the second one could be overridden in subclasses. 据我所知,在声明方法时静态关键字之间的主要区别在于第二个可以在子类中重写。

The problem 问题

However when I declare a protocol in Swift 1.2 like this: 但是,当我在Swift 1.2中声明一个协议时,如下所示:

protocol MyProtocol
{
    class func dummyClassMethod()
}

compiler gives an error: 编译器给出错误:

Class methods are only allowed within classes; 类方法只允许在类中; use 'static' to declare a static method 使用'static'来声明静态方法

The error is pretty descriptive as obviously MyProtocol is not a class, however I want to make a class func part of the protocol. 这个错误非常具有描述性,因为很明显MyProtocol不是一个类,但是我想让一个类func成为协议的一部分。

What I've tried 我试过的

I've found that if I declare interface in protocol as static , compiler is happy and I could use this static method in all classes that adopt this protocol: 我发现如果我将协议中的接口声明为静态 ,编译器很高兴我可以在采用此协议的所有类中使用此静态方法:

protocol MyProtocol
{
    static func dummyClassMethod()
}

The question 这个问题

So my question basically is is this right? 所以我的问题基本上就是这样吗? This declaration states that my class method cannot be overridden in children, however in my implementation I could write and use the following: 这个声明声明我的类方法不能在子节点中重写,但是在我的实现中我可以编写并使用以下内容:

class ClassA: MyProtocol
{
    class func dummyClassMethod() {

    }
}

class ClassB: ClassA
{
    override class func dummyClassMethod() {

    }
}

and now my dummyClassMethod is not static anymore... 现在我的dummyClassMethod不再是静态的......

  1. Compiler is Ok and everything works - but why? 编译器很好,一切正常 - 但为什么呢?

  2. Is it specific to the fact that interface itself is static, however it's implementation is not? 它是否特定于接口本身是静态的,但它的实现不是?

  3. Is there a better alternative for class func in protocols? 协议中的类func是否有更好的替代方案?

Objective-C solution Objective-C解决方案

In ObjC this is pretty easy and compile & run flawlessly: 在ObjC中,这非常简单,编译和运行完美:

@protocol MyProtocol 

+(void)dummyClassMethod;

@end

You can review Apple's Documentation (subsection Method Requirements ). 您可以查看Apple的文档 (小节方法要求 )。

There says: 有说:

As with type property requirements, you always prefix type method requirements with the static keyword when they are defined in a protocol. 与类型属性要求一样,在协议中定义static关键字时,始终使用static关键字为类型方法要求添加前缀。 This is true even though type method requirements are prefixed with the class or static keyword when implemented by a class 即使在实现时,类型方法要求以classstatic关键字为前缀,也是如此

In practice, You can do it as follow: 在实践中,您可以按照以下方式执行此操作:

First, declare your protocol: 首先,声明你的协议:

protocol SomeProtocol {
    static func someMethod()
}

Then, in your class you've 2 options : 然后,在你的class你有两个选择

First: 第一:

class SomeClass : SomeProtocol {
    class func someMethod()
}

Second: 第二:

class SomeClass : SomeProtocol {
    static func someMethod()
}

I hope, this may clarify your doubt.. 我希望,这可能澄清你的怀疑..

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html

A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. 协议定义了适合特定任务或功能的方法,属性和其他要求的蓝图。 The protocol doesn't actually provide an implementation for any of these requirements—it only describes what an implementation will look like. 该协议实际上并未提供任何这些要求的实现 - 它只描述了实现的样子。 The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements. 然后,可以通过类,结构或枚举来采用该协议,以提供这些要求的实际实现。

After this protocol definition it becomes reasonable that 在这个协议定义之后,它变得合理

As with type property requirements, you always prefix type method requirements with the static keyword when they are defined in a protocol. 与类型属性要求一样,在协议中定义static关键字时,始终使用static关键字为类型方法要求添加前缀。 This is true even though type method requirements are prefixed with the class or static keyword when implemented by a class... 即使类型方法要求在类实现时以类或静态关键字为前缀,这也是如此...

To make protocol method static and final implement that method with static keyword 要使协议方法静态,最后用static关键字实现该方法

class ClassA: MyProtocol{

    static func dummyClassMethod() {

    }
} 

and now you cant override dummyClassMethod function anymore. 现在你不能再覆盖dummyClassMethod函数了。 If you want to prevent overriding only you must declare protocol method as final. 如果要防止覆盖,则必须将协议方法声明为final。 About class functions, they were not fully supported in Swift 1.0 and now in Swift 1.2 I think that they are moving towards static functions 关于类函数,它们在Swift 1.0中并不完全支持,现在在Swift 1.2中我认为它们正在向静态函数发展

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM