Created
September 24, 2013 18:26
-
-
Save danmartyn/6689102 to your computer and use it in GitHub Desktop.
Encode and Decode objects without making typos
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
Taken From: http://stablekernel.com/blog/speeding-up-nscoding-with-macros/ | |
#define OBJC_STRINGIFY(x) @#x | |
#define encodeObject(x) [aCoder encodeObject:x forKey:OBJC_STRINGIFY(x)] | |
#define decodeObject(x) x = [aDecoder decodeObjectForKey:OBJC_STRINGIFY(x)] | |
#define encodeFloat(x) [aCoder encodeFloat:x forKey:OBJC_STRINGIFY(x)] | |
#define decodeFloat(x) x = [aDecoder decodeFloatForKey:OBJC_STRINGIFY(x)] | |
- (id)initWithCoder:(NSCoder *)aDecoder | |
{ | |
self = [super init]; | |
if(self) { | |
decodeObject(_obj); | |
decodeFloat(someFloat); | |
} | |
return self; | |
} | |
- (void)encodeWithCoder:(NSCoder *)aCoder | |
{ | |
encodeObject(_obj); | |
encodeFloat(someFloat); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment