TestsTested | ✓ |
LangLanguage | Objective C++Objective C++ |
License | MIT |
ReleasedLast Release | Oct 2015 |
Maintained by Victor Pavlychko.
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 policiesweak
attribute implemented using value holder objectgetter
and setter
specifications are properly parsedNSValue
or NSNumber
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
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, [email protected]
DynamicAssociatedProperties is available under the MIT license. See the LICENSE file for more info.