Extending UIKit Controls Through Associated Objects

Extensions are great for extending classes but unfortunately, they cannot contain stored properties. Luckily, there is a work around using Associated Objects. Below, I have used associated objects to add a callback property to a UISegmented control extension making it easier to get the selected index.

The usage of the new extension is shown below:

Enjoy!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store