什么时候在 Swift 中使用 `protocol` 和 `protocol: class`?

When to use `protocol` and `protocol: class` in Swift?

我已经设置了一个协议来将一些信息发送回之前的VC。

我是这样定义的:

protocol FilterViewControllerDelegate: class  {
    func didSearch(Parameters:[String: String]?)
}

但是使用时有什么区别:

protocol FilterViewControllerDelegate  {
        func didSearch(Parameters:[String: String]?)
    }

我什么时候应该使用 : class 协议?

意思是你定义的协议只能被类采纳,结构体和枚举都不能采纳

来自Official Swift book

protocol SomeClassOnlyProtocol: class, SomeInheritedProtocol {
    // class-only protocol definition goes here } 

In the example above, SomeClassOnlyProtocol can only be adopted by class types. It is a compile-time error to write a structure or enumeration definition that tries to adopt SomeClassOnlyProtocol.

Swift 4 版本

AnyObject 添加到这样的协议定义中

protocol FilterViewControllerDelegate: AnyObject  {
    func didSearch(parameters:[String: String]?)
}

意味着只有 class 才能遵守该协议。

鉴于此

protocol FilterViewControllerDelegate: AnyObject  {
    func didSearch(parameters:[String: String]?)
}

你会写这个

class Foo: FilterViewControllerDelegate {
    func didSearch(parameters:[String: String]?) { }
}

不是这个

struct Foo: FilterViewControllerDelegate {
    func didSearch(parameters:[String: String]?) { }
}

Swift 3 版本

:class 添加到这样的协议定义中

protocol FilterViewControllerDelegate: class  {
    func didSearch(Parameters:[String: String]?)
}

意味着只有 class 才能遵守该协议。

鉴于此

protocol FilterViewControllerDelegate: class  {
    func didSearch(Parameters:[String: String]?)
}

你会写这个

class Foo: FilterViewControllerDelegate {
    func didSearch(Parameters:[String: String]?) { }
}

不是这个

struct Foo: FilterViewControllerDelegate {
    func didSearch(Parameters:[String: String]?) { }
}

Swift 3.2 更新:

要声明 class 现在只写协议:

protocol SomeClassOnlyProtocol: AnyObject, SomeInheritedProtocol {
    // class-only protocol definition goes here
}

而不是

protocol SomeClassOnlyProtocol: class, SomeInheritedProtocol {
    // class-only protocol definition goes here
}

第二个片段现在似乎仍然有效。 参考:https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html

关于使用 class/AnyObject 关键字标记协议还有另一件事。

给定这样的协议:

Swift 4及以上(根据docs):

protocol FilterViewControllerDelegate: AnyObject  {
    func didSearch(with parameters: [String: String]?)
}

前Swift 4语法:

protocol FilterViewControllerDelegate: class  {
    func didSearch(with parameters: [String: String]?)
}


例如,假设您正在创建一个 DetailViewController,其委托 属性 类型为 FilterViewControllerDelegate

class DetailViewController: UIViewController {
    weak var delegate: FilterViewControllerDelegate
}

如果您没有使用 class 关键字标记该协议,您将无法将 delegate 属性 标记为 weak

为什么?

很简单 - 只有基于 class 的属性才能具有弱关系。 如果你想避免引用循环,那就是要走的路

Swift 5.1, Xcode 11 语法:

protocol FilterViewControllerDelegate: AnyObject {
       func didSearch(Parameters:[String: String]?)
}

这个协议只能被classes采用。

回答你的第一个问题 -

But what is the difference when using:

与此的区别:

protocol FilterViewControllerDelegate  {
        func didSearch(Parameters:[String: String]?)
}

是该协议可以采用值类型,例如枚举和结构。

回答你的第二个问题 -

And when should I use a : class protocal?

什么时候应该使用 class 协议我想描述委托模式的下一个例子: 想象一下你有委托协议。

protocol PopupDelegate: AnyObject {
    func popupValueSelected(value: String)
}

并且在另一个 class 中你想创建一个 属性

var delegate: PopupDelegate?

但这具有很强的参考意义,可能会给您带来内存泄漏问题。修复内存泄漏的一种方法是使委托 属性 - 弱。在我们不会让我们的协议仅适用于申请 classes 之前,Swift 认为我们也可以将我们的协议应用于值类型。

weak var delegate: PopupDelegate?

如果您尝试将委托声明为 weak,您将看到下一个错误:

'weak' var only be applied to class and class-bound protocol types, not 'PopupDelegate'

但是我们不能将 weak 应用于值类型。所以我们需要将我们的协议限制为引用类型,所以 swift 知道它是一个引用类型。 为了让您可以将此委托声明为弱委托,您需要限制您的协议仅供 classes 使用:

protocol PopupDelegate: AnyObject {
    func popupValueSelected(value: String)
}