gpt4 book ai didi

ios - swift 这些协议(protocol)有什么区别

转载 作者:行者123 更新时间:2023-11-28 09:59:05 25 4
gpt4 key购买 nike

我见过以两种方式声明协议(protocol),但我不明白其中的区别。

示例 1:

protocol AddItemViewControllerDelegate {
func controller(controller: AddItemViewController, didAddItem: String)
}

例子2:

protocol AddItemViewControllerDelegate: class {
func controller(controller: AddItemViewController, didAddItem: String)
}

那么有什么区别呢?

最佳答案

来自文档:

You can limit protocol adoption to class types (and not structures or enumerations) by adding the class keyword to a protocol’s inheritance list. The class keyword must always appear first in a protocol’s inheritance list, before any inherited protocols:

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

注意:

Use a class-only protocol when the behavior defined by that protocol’s requirements assumes or requires that a conforming type has reference semantics rather than value semantics.

关于ios - swift 这些协议(protocol)有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32126049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com