Swift 所选 class 实例的扩展
Swift extension for selected class instance
在 Objective-C 类别中,您可以通过在 class 中包含类别的 header 来引入类别方法引入的扩展功能。
似乎所有 Swift 扩展都是自动引入的,无需导入。你如何在 Swift 中实现同样的事情?
例如:
extension UIView {
// only want certain UIView to have this, not all
// similar to Objective-C, where imported category header
// will grant the capability to the class
func extraCapability() {
}
}
NOTE
Private access in Swift differs from private access in most other languages, as it’s scoped to the enclosing source file rather than to the enclosing declaration. This means that a type can access any private entities that are defined in the same source file as itself, but an extension cannot access that type’s private members if it’s defined in a separate source file.
根据 Apple 的说法,here,您似乎无法在单独的文件中将扩展设为私有。
您可以在同一源文件中创建私有扩展。
您可以通过在扩展前添加 private 来为特定 class 设置私有扩展
private extension UIView {
func extraCapability() {
}
}
这意味着它只能用于特定的 class。但是您需要将此添加到每个需要此扩展名的 class。据我所知,没有办法像在 Obj-c
中那样导入扩展
定义将用作选择的协议,无论扩展是否可用:
protocol UIViewExtensions { }
然后为协议定义一个扩展,但仅适用于 UIView
的子classes(反之亦然):
extension UIViewExtensions where Self: UIView {
func testFunc() -> String { return String(tag) }
}
定义为具有协议的 class 也将具有扩展名:
class A: UIView, UIViewExtensions { }
A().testFunc() //has the extension
如果没有定义协议,它也不会有扩展名:
class B: UIView {}
B().testFunc() //execution failed: MyPlayground.playground:17:1: error: value of type 'B' has no member 'testFunc'
更新
从开始,如果需要重写函数,我唯一能想到的就是subclass:
class UIViewWithExtensions: UIView {
override func canBecomeFocused() -> Bool { return true }
}
UIViewWithExtensions().canBecomeFocused() // returns true
这也可以与扩展结合使用,但我认为它仍然没有多大意义。
在 Objective-C 类别中,您可以通过在 class 中包含类别的 header 来引入类别方法引入的扩展功能。
似乎所有 Swift 扩展都是自动引入的,无需导入。你如何在 Swift 中实现同样的事情?
例如:
extension UIView {
// only want certain UIView to have this, not all
// similar to Objective-C, where imported category header
// will grant the capability to the class
func extraCapability() {
}
}
NOTE Private access in Swift differs from private access in most other languages, as it’s scoped to the enclosing source file rather than to the enclosing declaration. This means that a type can access any private entities that are defined in the same source file as itself, but an extension cannot access that type’s private members if it’s defined in a separate source file.
根据 Apple 的说法,here,您似乎无法在单独的文件中将扩展设为私有。
您可以在同一源文件中创建私有扩展。
您可以通过在扩展前添加 private 来为特定 class 设置私有扩展
private extension UIView {
func extraCapability() {
}
}
这意味着它只能用于特定的 class。但是您需要将此添加到每个需要此扩展名的 class。据我所知,没有办法像在 Obj-c
中那样导入扩展定义将用作选择的协议,无论扩展是否可用:
protocol UIViewExtensions { }
然后为协议定义一个扩展,但仅适用于 UIView
的子classes(反之亦然):
extension UIViewExtensions where Self: UIView {
func testFunc() -> String { return String(tag) }
}
定义为具有协议的 class 也将具有扩展名:
class A: UIView, UIViewExtensions { }
A().testFunc() //has the extension
如果没有定义协议,它也不会有扩展名:
class B: UIView {}
B().testFunc() //execution failed: MyPlayground.playground:17:1: error: value of type 'B' has no member 'testFunc'
更新
从
class UIViewWithExtensions: UIView {
override func canBecomeFocused() -> Bool { return true }
}
UIViewWithExtensions().canBecomeFocused() // returns true
这也可以与扩展结合使用,但我认为它仍然没有多大意义。