@interface Esame : NSObject{
NSString *nome;
int voto;
int crediti;
int anno;
}
@property (nonatomic, retain) NSString *nome;
- (id)initWithNome:(NSString*)nome voto:(int)voto crediti:(int)crediti anno:(int)anno;
@end
This is my implementation
#import "Esame.h"
@implementation Esame
@synthesize nome;
- (id)initWithNome:(NSString*)name voto:(int)voto crediti:(int)crediti anno:(int)anno {
if ((self = [super init])) {
self.nome = name;
}
return self;
}
- (id)initWithCoder:(NSCoder *)decoder {
if (self = [super init]) {
nome = [decoder decodeObjectForKey:@"nome"] ;
voto = [decoder decodeIntForKey:@"voto"];
crediti = [decoder decodeIntForKey:@"crediti"];
anno = [decoder decodeIntForKey:@"anno"];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder {
if (nome) [encoder encodeObject:nome forKey:@"nome"];
if (voto) [encoder encodeInt:voto forKey:@"voto"];
if (crediti) [encoder encodeInt:crediti forKey:@"crediti"];
if (anno) [encoder encodeInt:anno forKey:@"anno"];
}
@end
I receive same strange error… expecially in the NSString… what’s wrong?
Try removing your conditionals before your
encodeInt:; you should probably always encode all members. Also, you should probably declare that you conform to NSCoding with@interface Esame : NSObject<NSCoding>.If this doesn’t work, try posting the error message you’re seeing.