DynamicAssociatedProperties 0.1.0

DynamicAssociatedProperties 0.1.0

TestsTested
LangLanguage Objective C++Objective C++
License MIT
ReleasedLast Release Oct 2015

Maintained by Victor Pavlychko.



Introduction

DynamicAssociatedProperties library automatically generates accessors for @dynamic properties defined in categories using objc_getAssociatedObject/objc_setAssociatedObject for backing storage.

Supported property attributes:

  • strong, copy and assign attributes implemented using runtime association policies
  • weak attribute implemented using value holder object
  • custom getter and setter specifications are properly parsed
  • non-object typed are automatically boxed to NSValue or NSNumber
  • C++ templates are used to easily generate type-safe accessors for any value type

Supported types:

  • char
  • unsigned char
  • short
  • unsigned short
  • int
  • unsigned int
  • long
  • unsigned long
  • long long
  • unsigned long long
  • float
  • double
  • BOOL
  • CGPoint
  • CGVector
  • CGSize
  • CGRect
  • CGAffineTransform
  • UIEdgeInsets
  • UIOffset
  • CATransform3D
  • any custom value type you register yourself

Adding Properties in Category

To add properties in a category simply declate them in your *.h file as usual

@interface AWTestClass (AWTestCategory)

@property (copy)   NSString  *awStringProperty;
@property (weak)   id         awWeakProperty;
@property (assign) NSInteger  awIntegerProperty;
@property (assign) CGRect     awCGRectProperty;

@end

And define as @dynamic in your *.m file. Calling +[NSObject dynamicAssociatedPropertiesRegisterAll] will register all @dynamic properties for current class.

#include <DynamicAssociatedProperties/DynamicAssociatedProperties>

@implementation AWTestClass (AWTestCategory)

@dynamic awStringProperty;
@dynamic awWeakProperty;
@dynamic awIntegerProperty;
@dynamic awCGRectProperty;

+ (void)load
{
    [self dynamicAssociatedPropertiesRegisterAll];
}

@end

Adding Support for Custom Types

Custom value types can be easily registered but type safe solution will require Objective-C++ file here. Simply rename your implementation file to *.mm and follow this syntax:

#include <DynamicAssociatedProperties/AccessorBlockFactoryRegistry.h>

@implementation AWTestClass (AWTestCategory)

+ (void)load
{
    AccessorBlockFactoryRegistry::instance().registerGetterAndSetterFactories<MyStructType>();
}

@end

When adding missing SDK structs consider extending NSObject+DynamicAssociatedProperties.mm and submitting pull request ;)

Installation

DynamicAssociatedProperties is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "DynamicAssociatedProperties"

To run the example project, clone the repo, and run pod install from the Example directory first.

Alternatives

Several alternative projects are available:

Author

Victor Pavlychko, [email protected]

License

DynamicAssociatedProperties is available under the MIT license. See the LICENSE file for more info.