Boon Boon - 4 months ago 15x
Swift Question

Swift extension for selected class instance

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



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'


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.