ios - Swift 中的类协议(protocol)

标签 ios swift swift2 protocols

我希望我的一些类(不是全部)符合使用 'Class-Only Protocols'来自文档。我正在做的是

protocol RefreshData: class, ClassA, ClassB
{
    func updateController()
}

我得到了错误

non class type 'RefreshData cannot inherit from classA
non class type 'RefreshData cannot inherit from classB

我不确定我是否完全按照文档中的说明进行操作。有人对此有任何想法吗?

最佳答案

Swift 4 允许您组合类型,因此您可以拥有您的协议(protocol),然后创建,例如,类型别名以将其与特定类要求组合。

对于(一个人为的)例子:

typealias PresentableVC = UIViewController & Presentable

对于呈现的代码:

问题是您试图限制特定的类,而 Swift 无法做到这一点(目前无论如何)。您只能限制为类并从其他协议(protocol)继承。您的语法用于协议(protocol)继承,但您试图将其用作类限制。

注意类协议(protocol)的目的是:

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/38270635/

相关文章:

ios - 如何让 NSURLSession 从服务器返回 Content-Length(http header )。 Objective-C、iOS

ios - React Native Expo iOS 应用程序 - 尝试修改 info.plist

ios - 以编程方式设置约束导致 Swift 中的冲突

ios - 如何为横向和纵向创建单独的 UI?

iphone - UITextField - 圆角问题

ios - 使用文件管理器创建文件时如何在 UI 中显示事件指示器

ios - XCUI 测试 - 无法滚动到可见

ios - 使用 Coredata Swift 发送到实例的无法识别的选择器

uiview - @IBInspectable 不更新 Storyboard

ios - 高分辨率屏幕上的 UIButton 圆角半径