Skip to content

Instantly share code, notes, and snippets.

@dreampiggy
Created April 13, 2017 07:19
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dreampiggy/2f2da443874b329a2f5d12f546a7a0cf to your computer and use it in GitHub Desktop.
Save dreampiggy/2f2da443874b329a2f5d12f546a7a0cf to your computer and use it in GitHub Desktop.
Objective-C Category Property Macro
#ifndef TTD_CATEGORY_PROPERTY
#define TTD_CATEGORY_PROPERTY
#import <objc/runtime.h>
#define TTD_GET_PROPERTY(property) objc_getAssociatedObject(self, @selector(property));
#define TTD_SET_STRONG(property) objc_setAssociatedObject(self, @selector(property), property, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
#define TTD_SET_COPY(property) objc_setAssociatedObject(self, @selector(property), property, OBJC_ASSOCIATION_COPY_NONATOMIC);
#define TTD_SET_UNSAFE_UNRETAINED(property) objc_setAssociatedObject(self, @selector(property), property, OBJC_ASSOCIATION_ASSIGN);
#define TTD_SET_ASSIGN(property, value) objc_setAssociatedObject(self, @selector(property), value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
#define TTD_SET_WEAK(property) id __weak __weak_object = property; \
id (^__weak_block)() = ^{ return __weak_object; }; \
objc_setAssociatedObject(self, @selector(property), __weak_block, OBJC_ASSOCIATION_COPY);
#define TTD_GET_WEAK(property) objc_getAssociatedObject(self, @selector(property)) ? ((id (^)())objc_getAssociatedObject(self, @selector(property)))() : nil;
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment