Last active
December 16, 2015 10:09
-
-
Save aufflick/5418326 to your computer and use it in GitHub Desktop.
X Macro goodness for removing duplication in NSCoder implementations
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
typedef void (^XDecodeBlock)(id x); | |
typedef id (^XEncodeBlock)(void); | |
// posing a CLLocationCoordinate2D as a CGPoint because of a bug in NSValue valueWithMKCoordinate... | |
static XEncodeBlock XCLLocationCoordinate2DEncoder(CLLocationCoordinate2D loc) | |
{ | |
return ^{ | |
return [NSValue valueWithCGPoint:CGPointMake(loc.latitude, loc.longitude)]; | |
}; | |
} | |
static XDecodeBlock XCLLocationCoordinate2DDecoder(CLLocationCoordinate2D * loc) | |
{ | |
return ^(NSValue * v){ | |
CGPoint p = [v CGPointValue]; loc->latitude = p.x; loc->longitude = p.y; | |
}; | |
} | |
-(void) encodeWithCoder: (NSCoder*) coder | |
{ | |
#define X(xtype, xivar) [coder encode##xtype:xivar forKey:@#xivar]; | |
#define XX(xivar, xencode, xdecode) do { XEncodeBlock x = xencode; [coder encodeObject:x() forKey:@#xivar]; } while (0); | |
XEncodeIvars | |
#undef XX | |
#undef X | |
#ifdef XEncodeBlock | |
XEncodeBlock | |
#endif | |
} | |
-(id) initWithCoder: (NSCoder*) decoder | |
{ | |
#define X(xtype, xivar) xivar = [decoder decode##xtype##ForKey:@#xivar]; | |
#define XX(xivar, xencode, xdecode) do { id x = [decoder decodeObjectForKey:@#xivar]; XDecodeBlock y = xdecode; y(x); } while (0); | |
XEncodeIvars | |
#undef XX | |
#undef X | |
#ifdef XDecodeBlock | |
XDecodeBlock | |
#endif | |
return self; | |
} |
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
@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 |
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
@implementation Foo | |
#define XEncodeIvars \ | |
X(Bool, _aBool) \ | |
X(Bool, _anotherBool) \ | |
X(Integer, _anInteger) \ | |
X(Integer, _anotherInteger) \ | |
X(Object, _anObject) \ | |
X(Object, _anotherObject) \ | |
XX(_location, XCLLocationCoordinate2DEncoder(_location), XCLLocationCoordinate2DDecoder(&_location)) | |
#include "XSynthesizeCoders.h" | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment