为什么我们使用扩展?
Why we use Extension?
因为我们有面向对象编程,所以我们可以使父 class 具有所有子 class 所需的所有功能。那么扩展的目的是什么?我对这个问题有点困惑,请任何人帮助我。
扩展程序
在不使用 subclassing 的情况下向 class 添加函数,这在您没有实现要扩展的 class 的情况下非常有用,例如class框架或库中的元素
中所定义
Extensions add new functionality to an existing class, structure,
enumeration, or protocol type. This includes the ability to extend
types for which you do not have access to the original source code
(known as retroactive modeling). Extensions are similar to categories
in Objective-C. (Unlike Objective-C categories, Swift extensions do
not have names.)
Extensions in Swift can:
Add computed instance properties and computed type properties Define
instance methods and type methods Provide new initializers Define
subscripts Define and use new nested types Make an existing type
conform to a protocol In Swift, you can even extend a protocol to
provide implementations of its requirements or add additional
functionality that conforming types can take advantage of. For more
details, see Protocol Extensions.
NOTE
Extensions can add new functionality to a type, but they cannot
override existing functionality.
Extension Syntax
Declare extensions with the extension keyword:
extension SomeType {
// new functionality to add to SomeType goes here
}
An extension can extend an existing type to make it adopt one or more protocols. To
add protocol conformance, you write the protocol names the same way as
you write them for a class or structure:
extension SomeType: SomeProtocol, AnotherProtocol {
// implementation of protocol requirements goes here
}
Adding protocol conformance in this way is described in Adding
Protocol Conformance with an Extension.
An extension can be used to extend an existing generic type, as
described in Extending a Generic Type. You can also extend a generic
type to conditionally add functionality, as described in Extensions
with a Generic Where Clause.
希望这有助于澄清你
因为我们有面向对象编程,所以我们可以使父 class 具有所有子 class 所需的所有功能。那么扩展的目的是什么?我对这个问题有点困惑,请任何人帮助我。
扩展程序
在不使用 subclassing 的情况下向 class 添加函数,这在您没有实现要扩展的 class 的情况下非常有用,例如class框架或库中的元素
中所定义Extensions add new functionality to an existing class, structure, enumeration, or protocol type. This includes the ability to extend types for which you do not have access to the original source code (known as retroactive modeling). Extensions are similar to categories in Objective-C. (Unlike Objective-C categories, Swift extensions do not have names.)
Extensions in Swift can:
Add computed instance properties and computed type properties Define instance methods and type methods Provide new initializers Define subscripts Define and use new nested types Make an existing type conform to a protocol In Swift, you can even extend a protocol to provide implementations of its requirements or add additional functionality that conforming types can take advantage of. For more details, see Protocol Extensions.
NOTE
Extensions can add new functionality to a type, but they cannot override existing functionality.
Extension Syntax
Declare extensions with the extension keyword:
extension SomeType { // new functionality to add to SomeType goes here }
An extension can extend an existing type to make it adopt one or more protocols. To add protocol conformance, you write the protocol names the same way as you write them for a class or structure:
extension SomeType: SomeProtocol, AnotherProtocol { // implementation of protocol requirements goes here }
Adding protocol conformance in this way is described in Adding Protocol Conformance with an Extension.
An extension can be used to extend an existing generic type, as described in Extending a Generic Type. You can also extend a generic type to conditionally add functionality, as described in Extensions with a Generic Where Clause.
希望这有助于澄清你