Latest 1.0.0-beta.2
License MIT
Platforms ios 9.0, osx 10.9, watchos 2.0, tvos 9.0
Frameworks Foundation

add stored properties to existing types!

AssociatedObjects provides swift wrappers around objc_getAssociatedObject, objc_setAssociatedObject and objc_removeAssociatedObjects… that’s it

to expose the wrappers for your custom object, just extension MyObject: AssociatedObjects {} and enjoy! for convenience, NSObject already does this

typically we use associated objects to add stored properties to existing types…

extension UIView {

    // the key we'll associate `storedProperty` to
    private var storedPropertyKey: Character = "_"

    var storedProperty: T {
        get {
            // return the existing property if it exists, otherwise create a new one
            return getAssociatedObject(key: &UIView.storedPropertyKey) as? T ?? {
                let new = T()
                self.storedProperty = new
                return new
        set {
            setAssociatedObject(newValue, key: &UIView.storedPropertyKey)

a note on keys…

when passing a key to getAssociatedObject or setAssociatedObject, the pointer to that key is what is used to index the property.

// somewhat counter-intuitively, doing this doesn't work! 
a.setAssociatedObject(b, key: "🔑")
a.getAssociatedObject(key: "🔑")

// instead, you should pass the pointer to a 'constant' variable (a Character is recommended)
var key: Character = "🔑"
a.setAssociatedObject(b, key: &key)
a.getAssociatedObject(key: &key)

association policies…

setAssociatedObject takes an optional policy parameter. this determines how the new associated object should be referenced by the owner

enum AssociationPolicy {
    case assign 
    case copy 
    case copyNonatomic
    case retain
    case retainNonatomic 

these match 1:1 with objc_AssociationPolicy

Latest podspec

    "name": "AssociatedObjects",
    "version": "1.0.0-beta.2",
    "summary": "swift wrappers for objc's runtime associated objects",
    "description": "AssociatedObjects provides wrappers around `objc_getAssociatedObject`,n`objc_setAssociatedObject` and `objc_removeAssociedObjects` by way of protocol extensions.nnTo enable the wrappers for an object, just inherit from the AssociatedObjects protocolnand go crazy!",
    "homepage": "",
    "license": {
        "type": "MIT",
        "file": ""
    "authors": "James Taylor",
    "social_media_url": "",
    "platforms": {
        "ios": "9.0",
        "osx": "10.9",
        "watchos": "2.0",
        "tvos": "9.0"
    "source": {
        "git": "",
        "tag": "1.0.0-beta.2"
    "source_files": "AssociatedObjects/*.swift",
    "frameworks": "Foundation"

Pin It on Pinterest

Share This