Skip to content

Instantly share code, notes, and snippets.

@vickeryj
Created May 7, 2010 18:23
Show Gist options
  • Save vickeryj/393816 to your computer and use it in GitHub Desktop.
Save vickeryj/393816 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
#import "NSObject+PropertySupport.h"
@interface PropertyCoder : NSObject <NSCoding> {
}
@end
--
#import "PropertyCoder.h"
#define PROPERTY_KEY_PREFIX @"PCPropertyKeyPrefix"
@implementation PropertyCoder
#pragma mark NSCoding methods
- (void)encodeWithCoder:(NSCoder *)aCoder {
for (NSString *propertyKey in [[self class] propertyNames]) {
id propertyValue = [self valueForKey:propertyKey];
if ([propertyValue conformsToProtocol:@protocol(NSCoding)]) {
[aCoder encodeObject:propertyValue forKey:[PROPERTY_KEY_PREFIX stringByAppendingString:propertyKey]];
}
}
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
for (NSString *propertyKey in [[self class] propertyNames]) {
[self setValue:[aDecoder decodeObjectForKey:[PROPERTY_KEY_PREFIX stringByAppendingString:propertyKey]] forKey:propertyKey];
}
return self;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment