Problems with the NSKeyedArchiver
i've still have got problems with the NSKeyedArchiver. I implemented everything I was told to do, but it still does not work. I'm kind of frustrated. So could anyone help me out?
Here is the .h file:
#import <Foundation/Foundation.h>
#import "JFIdentifier.h" // This is my own class to create a unique identifier for every JKDataObject
@interfac开发者_如何学Ce JKDataObject : NSObject <NSCoding> {
NSUInteger identifier;
NSString *user;
NSDate *creationDate;
NSDate *alteredDate;
}
#pragma mark -
#pragma mark Setter
- (void)setIdentifier:(NSUInteger)newIdentifier silent:(BOOL)silent;
- (void)setUser:(NSString *)newUser silent:(BOOL)silent;
- (void)setCreationDate:(NSDate *)newCreationDate silent:(BOOL)silent;
- (void)setAlteredDate:(NSDate *)newAlteredDate;
#pragma mark -
#pragma mark Getter
- (NSUInteger)identifier;
- (NSString *)user;
- (NSDate *)creationDate;
- (NSDate *)alteredDate;
#pragma mark -
#pragma mark Methods
- (void)dataObjectWasAltered:(JKDataObject *)alteredObject;
#pragma mark -
@end
and this is the .m file:
#import "JKDataObject.h"
@implementation JKDataObject
#pragma mark -
#pragma mark Initiation
- (id)init {
[super init];
identifier = [JFIdentifier createIdentifier];
user = [[NSString alloc] initWithString:NSUserName()];
creationDate = [[NSDate alloc] initWithTimeIntervalSinceNow:0];
alteredDate = [[NSDate alloc] initWithTimeIntervalSinceNow:0];;
return self;
}
- (id)initWithCoder:(NSCoder *)decoder {
[super init];
identifier = [decoder decodeIntegerForKey:@"identifier"];
user = [[decoder decodeObjectForKey:@"user"] retain];
creationDate = [[decoder decodeObjectForKey:@"creationDate"] retain];
alteredDate = [[decoder decodeObjectForKey:@"alteredDate"] retain];
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder {
// [super encodeWithCoder:encoder];
[encoder encodeInteger:identifier forKey:@"identifier"];
[encoder encodeObject:user forKey:@"user"];
[encoder encodeObject:creationDate forKey:@"creationDate"];
[encoder encodeObject:alteredDate forKey:@"alteredDate"];
}
- (void)release {
[user release];
[creationDate release];
[alteredDate release];
[super release];
}
#pragma mark -
#pragma mark Setter
- (void)setIdentifier:(NSUInteger)newIdentifier silent:(BOOL)silent {
identifier = newIdentifier;
if (!silent) {
[self dataObjectWasAltered:self];
}
}
- (void)setUser:(NSString *)newUser silent:(BOOL)silent {
user = newUser;
if (!silent) {
[self dataObjectWasAltered:self];
}
}
- (void)setCreationDate:(NSDate *)newCreationDate silent:(BOOL)silent {
creationDate = newCreationDate;
if (!silent) {
[self dataObjectWasAltered:self];
}
}
- (void)setAlteredDate:(NSDate *)newAlteredDate {
alteredDate = newAlteredDate;
}
#pragma mark -
#pragma mark Getter
- (NSUInteger)identifier {
return identifier;
}
- (NSString *)user {
return user;
}
- (NSDate *)creationDate {
return creationDate;
}
- (NSDate *)alteredDate {
return alteredDate;
}
#pragma mark -
#pragma mark Methods
- (void)dataObjectWasAltered:(JKDataObject *)alteredObject {
alteredDate = [[NSDate alloc] initWithTimeIntervalSinceNow:0];
}
#pragma mark -
@end
Does anybody know what I did wrong?
Thanks!!
Two obvious bugs:
One thing you certainly did wrong here is that you called your dealloc
method release
instead. That will not work and will result in unexpected behaviour.
Also, in your init methods, you should do self = [super init]
instead of just [super init]
.
精彩评论