Skip to content

Instantly share code, notes, and snippets.

@dchohfi
Created June 20, 2012 15:05
Show Gist options
  • Save dchohfi/2960341 to your computer and use it in GitHub Desktop.
Save dchohfi/2960341 to your computer and use it in GitHub Desktop.
NSCoding protocol
#import <Foundation/Foundation.h>
@interface Contato : NSObject <NSCoding>
@property (nonatomic, retain) NSString *nome;
@property (nonatomic, retain) NSString *email;
@property (nonatomic, retain) NSString *endereco;
@property (nonatomic, retain) NSString *telefone;
@property (nonatomic, retain) NSString *site;
@property (nonatomic, retain) UIImage *imagem;
@end
#import "Contato.h"
@implementation Contato
@synthesize nome, site, email, imagem, endereco, telefone;
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:nome forKey:@"nome"];
[aCoder encodeObject:site forKey:@"site"];
[aCoder encodeObject:email forKey:@"email"];
[aCoder encodeObject:imagem forKey:@"imagem"];
[aCoder encodeObject:endereco forKey:@"endereco"];
[aCoder encodeObject:telefone forKey:@"telefone"];
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
if(self){
[self setNome:[aDecoder decodeObjectForKey:@"nome"]];
[self setEndereco:[aDecoder decodeObjectForKey:@"endereco"]];
[self setSite:[aDecoder decodeObjectForKey:@"site"]];
[self setImagem:[aDecoder decodeObjectForKey:@"imagem"]];
[self setEmail:[aDecoder decodeObjectForKey:@"email"]];
[self setTelefone:[aDecoder decodeObjectForKey:@"telefone"]];
}
return self;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment