Last active
September 1, 2016 22:02
-
-
Save richy486/5da686d5cd6c876a0c439c98c4f7bf95 to your computer and use it in GitHub Desktop.
Objective-c categories with properties.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Objective-c categories with properties. | |
// | |
// Taken from: | |
// http://stackoverflow.com/a/22812632/667834 | |
// http://stackoverflow.com/a/16020927/667834 | |
// http://stackoverflow.com/a/11529511/667834 | |
// | |
#define CATEGORY_PROPERTY_GET(type, property) \ | |
- (type) property { return objc_getAssociatedObject(self, @selector(property)); } | |
#define CATEGORY_PROPERTY_SET(type, property, setter) \ | |
- (void) setter (type) property { objc_setAssociatedObject(self, @selector(property), property, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } | |
#define CATEGORY_PROPERTY_GET_SET(type, property, setter) CATEGORY_PROPERTY_GET(type, property) CATEGORY_PROPERTY_SET(type, property, setter) | |
#define CATEGORY_PROPERTY_GET_NSNUMBER_PRIMITIVE(type, property, valueSelector) \ | |
- (type) property { return [objc_getAssociatedObject(self, @selector(property)) valueSelector]; } | |
#define CATEGORY_PROPERTY_SET_NSNUMBER_PRIMITIVE(type, property, setter, numberSelector) \ | |
- (void) setter (type) property { objc_setAssociatedObject(self, @selector(property), [NSNumber numberSelector: property], OBJC_ASSOCIATION_RETAIN_NONATOMIC); } | |
#define CATEGORY_PROPERTY_GET_UINT(property) CATEGORY_PROPERTY_GET_NSNUMBER_PRIMITIVE(unsigned int, property, unsignedIntValue) | |
#define CATEGORY_PROPERTY_SET_UINT(property, setter) CATEGORY_PROPERTY_SET_NSNUMBER_PRIMITIVE(unsigned int, property, setter, numberWithUnsignedInt) | |
#define CATEGORY_PROPERTY_GET_SET_UINT(property, setter) CATEGORY_PROPERTY_GET_UINT(property) CATEGORY_PROPERTY_SET_UINT(property, setter) | |
#define CATEGORY_PROPERTY_GET_BOOL(property) CATEGORY_PROPERTY_GET_NSNUMBER_PRIMITIVE(BOOL, property, boolValue) | |
#define CATEGORY_PROPERTY_SET_BOOL(property, setter) CATEGORY_PROPERTY_SET_NSNUMBER_PRIMITIVE(BOOL, property, setter, numberWithBool) | |
#define CATEGORY_PROPERTY_GET_SET_BOOL(property, setter) CATEGORY_PROPERTY_GET_BOOL(property) CATEGORY_PROPERTY_SET_BOOL(property, setter) | |
#define CATEGORY_PROPERTY_GET_NSVALUE_PRIMITIVE(type, property, valueSelector) \ | |
- (type) property { return [objc_getAssociatedObject(self, @selector(property)) valueSelector]; } | |
#define CATEGORY_PROPERTY_SET_NSVALUE_PRIMITIVE(type, property, setter, valueSelector) \ | |
- (void) setter (type) property { objc_setAssociatedObject(self, @selector(property), [NSValue valueSelector: property], OBJC_ASSOCIATION_RETAIN_NONATOMIC); } | |
#define CATEGORY_PROPERTY_GET_SEL(property) CATEGORY_PROPERTY_GET_NSNUMBER_PRIMITIVE(SEL, property, pointerValue) | |
#define CATEGORY_PROPERTY_SET_SEL(property, setter) CATEGORY_PROPERTY_SET_NSNUMBER_PRIMITIVE(SEL, property, setter, valueWithPointer) | |
#define CATEGORY_PROPERTY_GET_SET_SEL(property, setter) CATEGORY_PROPERTY_GET_SEL(property) CATEGORY_PROPERTY_SET_SEL(property, setter) | |
#import "Something.h" | |
@interface Something (Properties) | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "Something+Properties.h" | |
#import <objc/runtime.h> | |
@interface Something () | |
@property (nonatomic) BOOL thing; | |
@end | |
@implementation Something (Properties) | |
CATEGORY_PROPERTY_GET_SET_BOOL(thing, setThing:); | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment