Latest 1.0
Homepage https://github.com/markiv/NSObject-AssociatedDictionary
License MIT
Authors

A light-weight Objective-C category that adds a mutable dictionary for arbitrary, ad-hoc properties that can be associated with any object. The dictionary, called properties, is created just-in-time (the first time it’s accessed).

For any object, you can now set an ad-hoc property without prior declaration:

someObject.properties[@"someKey"] = someOtherObject;

Ever wished you could attach an object to a standard class without subclassing? Now you can:

#import <NSObject+AssociatedDictionary.h>
:
UIButton *deleteButton;
UITextField *accountField;
:
deleteButton.properties[@"confirmationMessage"] = @"Are you sure you want to do this?";
accountField.properties[@"inputMask"] = @"99-99999-9";
accountField.properties[@"required"]  = @YES;

You can even set properties like these directly in Xcode’s interface builder, using key paths and dot notation:

If NSObject+AssociatedDictionary.m is in your project, the UI loader will automatically set the properties for you. Without any additional initialization code, you’ll be able to access them like this:

if (!accountField.hasText && [accountField.properties[@"required"] boolValue]) {
    accountField.backgroundColor = accountField.properties[@"alertColor"];
    [self alert:accountField.properties[@"alertMessage"]];
}

Installation

Install via Cocoapods. Here’s a sample Podfile:

pod 'NSObject+AssociatedDictionary'

Alternatively, just drop the two files (NSObject+AssociatedDictionary.m and NSObject+AssociatedDictionary.h) into your project tree.

Contact

Vikram Kriplaney

License

NSObject+AssociatedDictionary is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "NSObject+AssociatedDictionary",
    "version": "1.0",
    "license": "MIT",
    "homepage": "https://github.com/markiv/NSObject-AssociatedDictionary",
    "authors": {
        "Vikram Kriplaney": "[email protected]"
    },
    "summary": "Adds a mutable dictionary for arbitrary, ad-hoc properties that can be associated with any object.",
    "description": "                   A light-weight Objective-C category that adds a mutable dictionary for arbitrary, ad-hoc properties that can be associated with any object. The dictionary, called properties, is created just-in-time (the first time it's accessed).n",
    "source": {
        "git": "https://github.com/markiv/NSObject-AssociatedDictionary.git",
        "tag": "1.0"
    },
    "source_files": "*.{h,m}",
    "requires_arc": true
}

Pin It on Pinterest

Share This