Avn*_*arr 6 generics protocols ios swift
在objective-c中,我可以声明一个返回类型的方法:
-(UIView<MyProtocol> *)someMethod;
Run Code Online (Sandbox Code Playgroud)
在此示例中,该方法返回UIView符合协议的方法MyProtocol.
我想在swift中做类似的事情:
protocol MyProtocol {
var someProperty : Int {get set}
}
protocol MyDelegate {
func someMethod() -> UIView : MyProtocol // the view should conform to the protocol - I don't care what kind of view it is - I don't want to define a specific type of view
}
Run Code Online (Sandbox Code Playgroud)
一般而言 - 委托应返回UIView带有var" someProperty"的
我不希望定义一个具体的UIView类.我希望用户能够返回任何类型的UIView(只要它符合协议)
我写的语法无效 - 我该怎么写呢?
这个问题是在 swift-ui 出现之前写的
“some”关键字通过允许从函数返回不透明类型来解决这个问题
protocol MyDelegate {
func someMethod() -> some MyProtocol
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1066 次 |
| 最近记录: |