CJAAssociatedObject 1.0.0

CJAAssociatedObject 1.0.0

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Dec 2014

Maintained by Carl Jahn.



  • By
  • Carl Jahn

A wrapper around the objc_getAssociatedObject and objc_setAssociatedObject from the <objc/runtime.h>

But why... ?

There is one important reason for this library:
You can add custom properties to your own Categories

Installation

Just drag & drop the NSObject+CJAAssociatedObject.h and NSObject+CJAAssociatedObject.m files to your project.

Usage

First of all take a look at the Example Project, especially at the UIViewController+ExampleCategory Category

static void *kExampleStringKey;
static void *kExampleBoolKey;
static void *kExampleFloatKey;
static void *kExampleDoubleKey;

- (void)yourMethod {

//Set a example String value for the given key
[self setAssociatedValue:@"Lorem ipsum" forKey:&kExampleStringKey];

//Gets the string value from the given key
NSString *exampleString = [self associatedValueForKey: &kExampleStringKey];
NSLog(@"example String %@", exampleString);

//Set a example BOOL value for the given key
[self setAssociatedBoolValue:YES forKey: &kExampleBoolKey];

//Gets the bool value from the given key
BOOL exampleBool = [self associatedBoolValueForKey: &kExampleBoolKey];
NSLog(@"example BOOL %d", exampleBool);

//Set a example float value for the given key
[self setAssociatedFloatValue:41.0f forKey: &kExampleFloatKey];

//Gets the float value from the given key
float exampleFloat = [self associatedFloatValueForKey: &kExampleFloatKey];
NSLog(@"example Float %f", exampleFloat);

//Set a example double value for the given key
[self setAssociatedDoubleValue: 37.37 forKey: &kExampleDoubleKey];

//Gets the double value from the given key
double exampleDouble = [self associatedDoubleValueForKey: &kExampleDoubleKey];
NSLog(@"example Double %f", exampleDouble);
}

LICENSE

Released under the MIT LICENSE