JacobSyndeo / Soccer

A small protocol and extension to help you tap into the Objective-C Runtime's powerful Associated Objects system from within Swift.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Soccer ⚽️

A small protocol and extension to help you tap into the Objective-C Runtime's powerful Associated Objects1 system from within Swift.

For example, suppose you wanted to extend UIAlertAction, and add a new property to it.

"Impossible!", you say? Just watch.

fileprivate struct CustomProperties {
    static var icon: UIAlertAction.Icon? = nil
}

extension UIAlertAction: SoccerPropertyStoring {
    public typealias T = Icon
    
    private var _icon: Icon? {
        get { // No stored property support in extensions? Pah, simpletons. Associated Objects to the rescue!
            return getAssociatedObject(&CustomProperties.icon)
        }
        set { // All hail the Obj-C runtime!
            return setAssociatedObject(&CustomProperties.icon, newValue: newValue)
        }
    }
}

Et voilà… you can now get and set a property in an extension of an object.

"What sorcery is this‽", you cry out. To which, I reply… "Just the Objective-C runtime happily working its magic."

As with any great power, this comes with great responsibility. Use it wisely, use it judiciously, use it appropriately.

Footnotes

  1. Soccer, being short for "Association Football", was the only (SFW) name I could come up related to "associated". Oh well… 🙃

About

A small protocol and extension to help you tap into the Objective-C Runtime's powerful Associated Objects system from within Swift.

License:Mozilla Public License 2.0


Languages

Language:Swift 100.0%