Skip to content

Instantly share code, notes, and snippets.

@orj
Forked from aufflick/XSynthesizeCoders.h
Last active December 16, 2015 10:09
Show Gist options
  • Save orj/5418430 to your computer and use it in GitHub Desktop.
Save orj/5418430 to your computer and use it in GitHub Desktop.
-(void) encodeWithCoder: (NSCoder*) coder
{
#define X(type, ivar) [coder encode##type:ivar forKey:@#ivar];
#define BOX(type, ivar) [coder encodeObject:([NSValue valueWith##type:ivar]) forKey:@#ivar];
XEncodeIvars
#undef BOX
#undef X
}
-(id) initWithCoder: (NSCoder*) decoder
{
#define X(type, ivar) ivar = [decoder decode##type##ForKey:@#ivar];
#define BOX(type, ivar) ivar = [[decoder decodeObjectForKey:@#ivar] type##Value];
XEncodeIvars
#undef BOX
#undef X
return self;
}
@interface Foo : NSObject <NSCoder>
@property BOOL aBool;
@property BOOL anotherBool;
@property NSInteger anInteger;
@property NSInteger anotherInteger;
@property NSObject * anObject;
@property NSObject * anotherObject;
@property CLLocationCoordinate2D location;
@end
@implementation Foo
#define XEncodeIvars \
X(Bool, _aBool) \
X(Bool, _anotherBool) \
X(Integer, _anInteger) \
X(Integer, _anotherInteger) \
X(Object, _anObject) \
X(Object, _anotherObject) \
BOX(MKCoordinate, _location))
#include "XSynthesizeCoders.h"
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment