ios 检查 Swift 中是否实现了可选协议方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30398786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if optional protocol method is implemented in Swift?
提问by confile
I have a swift protocol:
我有一个快速的协议:
@objc protocol SomeDelegate {
optional func myFunction()
}
I one of my classes I did:
我上过一堂课:
weak var delegate: SomeDelegate?
Now I want to check if the delegate
has myFunction
implemented.
现在我想检查是否delegate
已myFunction
实施。
In objective-c I can do:
在objective-c我可以做:
if ([delegate respondsToSelector:@selector(myFunction)]) {
...
}
But this is not available in Swift.
但这在 Swift 中不可用。
Edit: This is different from: What is the swift equivalent of respondsToSelector?I focus on class protocols not on classes.
编辑:这不同于:什么是respondsToSelector的swift等价物?我专注于课程协议而不是课程。
How do I check if my delegate has an optional method implemented?
如何检查我的委托是否实现了可选方法?
采纳答案by Tommy
Per The Swift Programming Language:
You check for an implementation of an optional requirement by writing a question mark after the name of the requirement when it is called, such as someOptionalMethod?(someArgument). Optional property requirements, and optional method requirements that return a value, will always return an optional value of the appropriate type when they are accessed or called, to reflect the fact that the optional requirement may not have been implemented.
您可以通过在调用需求名称后写一个问号来检查可选需求的实现,例如 someOptionalMethod?(someArgument)。可选属性要求和返回值的可选方法要求在访问或调用它们时将始终返回适当类型的可选值,以反映可选要求可能尚未实现的事实。
So the intention is not that you check whether the method is implemented, it's that you attempt to call it regardless and get an optional back.
因此,目的不是要检查该方法是否已实现,而是要无论如何尝试调用它并获得可选的返回值。
回答by newacct
You can do
你可以做
if delegate?.myFunction != nil {
}
回答by Chris
I've found it successful to add an extension to the protocol that defines basic default implementation and then any class implementing the protocol need only override the functions of interest.
我发现向定义基本默认实现的协议添加扩展是成功的,然后实现该协议的任何类只需要覆盖感兴趣的功能。
public protocol PresenterDelegate : class {
func presenterDidRefreshCompleteLayout(presenter: Presenter)
func presenterShouldDoSomething(presenter: Presenter) -> Bool
}
then extend
然后扩展
extension PresenterDelegate {
public func presenterDidRefreshCompleteLayout(presenter: Presenter) {}
public func presenterShouldDoSomething(presenter: Presenter) -> Bool {
return true
}
}
Now any class needing to conform to the PresenterDelegate protocol has all functions already implemented, so it's now optionalto override it's functionality.
现在任何需要符合 PresenterDelegate 协议的类都已经实现了所有功能,所以现在可以选择覆盖它的功能。
回答by Darshit Shah
Declaration
宣言
@objc public protocol nameOfDelegate: class {
@objc optional func delegateMethod(_ varA: int, didSelect item: Item)
}
Implimetation
暗示
if let delegate = nameOfDelegate {
delegate.delegateMethod?(1, didDeselect: node)
}
回答by Sergio Estevao
I normally implement it like this:
我通常是这样实现的:
self.delegate?.myFunction?()
if the delegate methods returns a value:
如果委托方法返回一个值:
var result = defaultValue
if let delegateResult = self.delegate?.myFunction?() else {
result = delegateResult
}
//do something with result