Last active
December 31, 2015 08:19
-
-
Save JohnEstropia/7959314 to your computer and use it in GitHub Desktop.
Synthesizing properties in Objective-C categories (simple version)
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
// | |
// _JEAssociatedObjectsWeakWrapper.h | |
// JEToolkit | |
// | |
// Created by John Rommel Estropia on 2013/12/14. | |
// Copyright (c) 2013 John Rommel Estropia. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
@interface _JEAssociatedObjectsWeakWrapper : NSObject | |
@property (nonatomic, weak, readonly) id weakObject; | |
- (id)initWithWeakObject:(id)weakObject; | |
@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
// | |
// _JEAssociatedObjectsWeakWrapper.m | |
// JEToolkit | |
// | |
// Created by John Rommel Estropia on 2013/12/14. | |
// Copyright (c) 2013 John Rommel Estropia. All rights reserved. | |
// | |
#import "_JEAssociatedObjectsWeakWrapper.h" | |
@implementation _JEAssociatedObjectsWeakWrapper | |
- (id)initWithWeakObject:(id)weakObject | |
{ | |
self = [super init]; | |
if (!self) | |
{ | |
return nil; | |
} | |
_weakObject = weakObject; | |
return self; | |
} | |
@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
// | |
// JEAssociatedObjects.h | |
// JEToolkit | |
// | |
// Created by John Rommel Estropia on 2013/10/26. | |
// Copyright (c) 2013 John Rommel Estropia. All rights reserved. | |
// | |
#import <objc/runtime.h> | |
#import "_JEAssociatedObjectsWeakWrapper.h" | |
#ifndef JEToolkit_JEAssociatedObjects_h | |
#define JEToolkit_JEAssociatedObjects_h | |
/*! A macro that implements the accessor and mutator methods specified. | |
Usage: | |
JESynthesize(assign, NSInteger, index, setIndex); | |
JESynthesize(strong, NSString *, name, setName); | |
JESynthesize(copy, void(^)(void), completion, setCompletion); | |
JESynthesize(unsafe_unretained, id, unsafeObject, setUnsafeObject); | |
JESynthesize(weak, id<UITableViewDelegate>, delegate, setDelegate); | |
JESynthesize(strong, NSString *, readonlyID, changeReadonlyID); | |
*/ | |
#define JESynthesize(ownership, type, getter, setter) \ | |
static const void *_JESynthesizeKey_##getter = &_JESynthesizeKey_##getter; \ | |
\ | |
- (type)getter \ | |
{ \ | |
return _JESynthesize_get_##ownership(type, getter); \ | |
} \ | |
\ | |
- (void)setter:(type)getter \ | |
{ \ | |
_JESynthesize_set_##ownership(type, getter); \ | |
} | |
#define _JESynthesize_get_assign(type, getter) \ | |
({ \ | |
/* We use an array so the initializer syntax will give us a nice zeroed-out value as default. */ \ | |
typeof(type) _je_value[1] = {}; \ | |
[(NSValue *)objc_getAssociatedObject(self, _JESynthesizeKey_##getter) getValue:_je_value]; \ | |
_je_value[0]; \ | |
}) | |
#define _JESynthesize_get_unsafe_unretained(type, getter) \ | |
objc_getAssociatedObject(self, _JESynthesizeKey_##getter); | |
#define _JESynthesize_get_strong _JESynthesize_get_unsafe_unretained | |
#define _JESynthesize_get_retain _JESynthesize_get_unsafe_unretained | |
#define _JESynthesize_get_copy _JESynthesize_get_unsafe_unretained | |
#define _JESynthesize_get_weak(type, getter) \ | |
((_JEAssociatedObjectsWeakWrapper *)objc_getAssociatedObject(self, _JESynthesizeKey_##getter)).weakObject | |
#define _JESynthesize_set_assign(type, getter) \ | |
objc_setAssociatedObject(self, \ | |
_JESynthesizeKey_##getter, \ | |
[[NSValue alloc] initWithBytes:&getter objCType:@encode(type)], \ | |
OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
#define _JESynthesize_set_unsafe_unretained(type, getter) \ | |
objc_setAssociatedObject(self, \ | |
_JESynthesizeKey_##getter, \ | |
getter, \ | |
OBJC_ASSOCIATION_ASSIGN); | |
#define _JESynthesize_set_strong(type, getter) \ | |
objc_setAssociatedObject(self, \ | |
_JESynthesizeKey_##getter, \ | |
getter, \ | |
OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
#define _JESynthesize_set_retain _JESynthesize_set_strong | |
#define _JESynthesize_set_copy(type, getter) \ | |
objc_setAssociatedObject(self, \ | |
_JESynthesizeKey_##getter, \ | |
getter, \ | |
OBJC_ASSOCIATION_COPY_NONATOMIC); | |
#define _JESynthesize_set_weak(type, getter) \ | |
objc_setAssociatedObject(self, \ | |
_JESynthesizeKey_##getter, \ | |
[[_JEAssociatedObjectsWeakWrapper alloc] initWithWeakObject:getter], \ | |
OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment