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/5418338 to your computer and use it in GitHub Desktop.
Save orj/5418338 to your computer and use it in GitHub Desktop.
-(void) encodeWithCoder: (NSCoder*) coder
{
#define X(type, ivar) [coder encode##type:ivar forKey:@#ivar];
XEncodeIvars
#undef X
}
-(id) initWithCoder: (NSCoder*) decoder
{
#define X(type, ivar) ivar = [decoder decode##type##ForKey:@#ivar];
XEncodeIvars
#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;
@end
@implementation Foo
#define XEncodeIvars \
X(Bool, _aBool) \
X(Bool, _anotherBool) \
X(Integer, _anInteger) \
X(Integer, _anotherInteger) \
X(Object, _anObject) \
X(Object, _anotherObject)
#include "XSynthesizeCoders.h"
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment