小编Rom*_*ich的帖子

使用默认参数值声明协议功能

我希望此功能在协议中:

func slideToRight(currentViewController viewController: UIViewController, completion: ((Bool)->())? = nil) {
// do some stuff
}
Run Code Online (Sandbox Code Playgroud)

但是当我写这样的协议时:

protocol SomeDelegate { 
func slideToRight(currentViewController viewController: UIViewController, completion: ((Bool)->())? = nil) 
}
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

协议方法中不允许使用默认参数

我知道,我可以这样定义签名:

protocol SomeDelegate { 
func slideToRight(currentViewController viewController: UIViewController, completion: ((Bool)->())?) 
}
Run Code Online (Sandbox Code Playgroud)

但是然后,我将无法调用缺少“ completion”字样的函数:

slideToRight(currentViewController viewController: vc)
Run Code Online (Sandbox Code Playgroud)

callback optional swift completion swift-protocols

5
推荐指数
1
解决办法
2070
查看次数

标签 统计

callback ×1

completion ×1

optional ×1

swift ×1

swift-protocols ×1