DynamicAssociatedProperties library automatically generates accessors for @dynamic
properties defined in categories using objc_getAssociatedObject
/objc_setAssociatedObject
for backing storage.
Supported property attributes:
strong
,copy
andassign
attributes implemented using runtime association policiesweak
attribute implemented using value holder object- custom
getter
andsetter
specifications are properly parsed - non-object typed are automatically boxed to
NSValue
orNSNumber
- 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
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
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 ;)
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.
Several alternative projects are available:
Victor Pavlychko, victor.pavlychko@gmail.com
DynamicAssociatedProperties is available under the MIT license. See the LICENSE file for more info.