在Objective-C中,可以将符合协议的类指定为方法参数 . 例如,我可以有一个方法只允许 UIViewController
符合 UITableViewDataSource
:
- (void)foo:(UIViewController<UITableViewDataSource> *)vc;
我可以't find a way to do this in Swift (perhaps it'不可能) . 您可以使用 func foo(obj: protocol<P1, P2>)
指定多个协议,但是如何要求对象也是特定类?
6 回答
Note in September 2015 :这是斯威夫特早期的观察 .
这似乎是不可能的 . Apple也对他们的一些API有这种烦恼 . 以下是iOS 8中新引入的一个示例(从beta 5开始):
UIInputViewController
的textDocumentProxy
property :在Objective-C中定义如下:
在斯威夫特:
链接到Apple的文档:https://developer.apple.com/library/prerelease/iOS/documentation/UIKit/Reference/UIInputViewController_Class/index.html#//apple_ref/occ/instp/UIInputViewController/textDocumentProxy
这个怎么样?:
在Swift 4中,您可以通过新的&sign实现此目的:
您可以将
foo
定义为泛型函数,并使用类型约束来同时要求类和协议 .Swift 4
Swift 3 (适用于Swift 4)
Swift 2
Swift书籍文档建议您使用带有where子句的类型约束:
这保证了“inParam”的类型为“SomeClass”,条件是它也遵循“SomeProtocol” . 您甚至可以指定逗号分隔的多个where子句:
使用Swift 3,您可以执行以下操作: