/// Protocol has empty default implementation of the following methods making them optional to implement:
/// `cancel()`
protocol Cancelable {
/// default implementation is empty.
func cancel()
}
extension Cancelable {
func cancel() {}
}
class Plane: Cancelable {
//Since cancel() have default implementation, that is optional to class Plane
}
let plane = Plane()
plane.cancel()
// Print out *United Airlines can't cancelable*
struct Info {
var height: Int
var weight: Int
}
@objc protocol Health {
func isInfoHealthy(info: Info) -> Bool
}
//Error "Method cannot be marked @objc because the type of the parameter cannot be represented in Objective-C"
为了声明与swift一起使用的可选协议,将函数声明为变量而不是func .
protocol Health {
var isInfoHealthy: (Info) -> (Bool)? { get set }
}
然后按如下方式实施协议
class Human: Health {
var isInfoHealthy: (Info) -> (Bool)? = { info in
if info.weight < 200 && info.height > 72 {
return true
}
return false
}
//Or leave out the implementation and declare it as:
//var isInfoHealthy: (Info) -> (Bool)?
}
然后你可以用“?”检查功能是否已实施
func returnEntity() -> Health {
return Human()
}
var anEntity: Health = returnEntity()
var isHealthy = anEntity.isInfoHealthy(Info(height: 75, weight: 150))?
//"isHealthy" is true
16 回答
在 Swift 3.0
它会节省您的时间 .
您需要在每个方法之前添加
optional
关键字 .但请注意,为此, your protocol must be marked with the @objc attribute.
这进一步意味着该协议适用于类而不适用于结构 .
具有协议继承的纯Swift方法:
Here is a concrete example with the delegation pattern.
设置您的协议:
将委托设置为类并实现协议 . 请注意,不需要实现可选方法 .
一个重要的是可选方法是可选的,需要一个“?”在打电话时 . 提到第二个问号 .
由于有一些关于如何使用 optional modifier 和 @objc attribute 定义可选需求协议的答案,我将举例说明如何使用协议扩展定义可选协议 .
下面的代码是Swift 3. * .
请注意协议扩展方法可以't invoked by Objective-C code, and worse is Swift team won'解决它 . https://bugs.swift.org/browse/SR-492
如果要使用可选方法, must 用
@objc
属性标记您的协议:否则,Swift协议的行为类似于其他OO语言上的接口,其中必须实现接口中定义的所有方法 .
在Swift 2及更高版本中,可以添加协议的默认实现 . 这为协议中的可选方法创建了一种新方法 .
它在创建可选协议方法方面不是一个非常好的方法,但是您可以在协议回调中使用结构 .
我在这里写了一个小小的总结:https://www.avanderlee.com/swift-2-0/optional-protocol-methods/
这里涉及将协议标记为“@objc”的其他答案在使用特定于swift的类型时不起作用 .
为了声明与swift一起使用的可选协议,将函数声明为变量而不是func .
然后按如下方式实施协议
然后你可以用“?”检查功能是否已实施
为了说明Antoine答案的机制:
从原始问题略微偏离主题,但它 Build 了Antoine的想法,我认为它可能会帮助某人 .
对于具有协议扩展的结构,您还可以使计算属性可选 .
您可以在协议上创建属性可选
在协议扩展中实现虚拟计算属性
现在,您可以使用已实现或未实现可选属性的结构
我还写了如何做optional properties in Swift protocols on my blog,我将不断更新,以防万一通过Swift 2版本发生变化 .
我想在问 how 之前你可以实现一个可选的协议方法,你应该问 why 你应该实现一个 .
如果我们将swift协议视为经典面向对象编程中的接口,则可选方法没有多大意义,也许更好的解决方案是创建默认实现,或将协议分成一组协议(可能具有一些继承关系)它们之间)代表协议中可能的方法组合 .
有关进一步阅读,请参阅https://useyourloaf.com/blog/swift-optional-protocol-methods/,其中提供了关于此问题的出色概述 .
这是一个非常简单的示例,仅适用于swift类,而不适用于结构或枚举 . 请注意,协议方法是可选的,有两个级别的可选链接 . 采用该协议的类在其声明中也需要@objc属性 .
如果你想在纯swift中执行它,最好的方法是提供一个默认的实现粒度,如果你返回一个Swift类型,例如 struct 与Swift类型
例如:
然后你可以实现协议而不定义每个 func
How to create optional and required delegate methods.
要在swift中定义
Optional
Protocol
,您应该在Protocol
声明之前使用@objc
关键字并在该协议中使用attribute
/method
声明 . 以下是协议的可选属性示例 .将
@optional
放在方法或属性的前面 .