Swift extension for selected class instance

自作多情 提交于 2021-02-16 06:24:23

问题


In Objective-C category, you can bring in the extended capability introduced by the category methods by including the header of the category in your class.

It seems like all Swift extensions are automatically introduced without import. How do you achieve the same thing in Swift?

For example:

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() {

  }
}

回答1:


Define a protocol that will serve as a selection, wether the extensions should be available or not:

protocol UIViewExtensions { }

then define an extension for the protocol, but only for subclasses of UIView (the other way around won't work):

extension UIViewExtensions where Self: UIView {
    func testFunc() -> String { return String(tag) }
}

A class that is defined to have the protocol will also have the extension:

class A: UIView, UIViewExtensions { }    
A().testFunc() //has the extension

And if it is not defined to have the protocol, it will also not have the extension:

class B: UIView {}    
B().testFunc() //execution failed: MyPlayground.playground:17:1: error: value of type 'B' has no member 'testFunc'

UPDATE

Since protocol extensions don't do class polymorphism, if you need to override functions, the only thing I can think of is to subclass:

class UIViewWithExtensions: UIView {
    override func canBecomeFocused() -> Bool { return true }
}
UIViewWithExtensions().canBecomeFocused() // returns true

this could also be combined with the extension, but I don't think it would still make much sense anymore.




回答2:


You can make extensions private for a particular class by adding private before the extension like so

private extension UIView {

  func extraCapability() {

  }
}

This will mean it can only be used in that particular class. But you will need to add this to each class that requires this extension. As far as I know there is no way to import the extension like you can in Obj-c




回答3:


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.

According to Apple, here, it does not appear you can make extensions private in separate files.

You can create a private extension in the same source file.



来源:https://stackoverflow.com/questions/37682420/swift-extension-for-selected-class-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!