Subclassing vs Extension in swift

As a general rule of thumb (YMMV):

  • Are you adding general-purpose functionalities that should be available to every UITextField? If so, make an extension. All UITextField instances can call the new methods.
  • Are you adding functionality that should be restricted to special instances of UITextField that you would identify precisely? If so, make a subclass. Only the instances of the subclass can use the new methods.

There are other technical considerations, like extensions can't add fields, for instance.


Swift extensions can't be overridden. So If you would like to create a testing purpose subclasses then you will be limited.

Tags:

Swift