NSKeyedArchiver 的问题
我的 NSKeyedArchiver 仍然有问题。 我执行了我被告知要做的一切,但它仍然不起作用。我有点沮丧。那么有人可以帮我吗?
这是 .h 文件:
#import <Foundation/Foundation.h>
#import "JFIdentifier.h" // This is my own class to create a unique identifier for every JKDataObject
@interface 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
这是 .m 文件:
#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
有人知道我做错了什么吗?
谢谢!!
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
@interface 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!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
两个明显的错误:
您在这里肯定做错的一件事是您调用了
dealloc
方法release
。这不会起作用,并且会导致意外的行为。另外,在 init 方法中,您应该执行
self = [super init]
而不仅仅是[super init]
。Two obvious bugs:
One thing you certainly did wrong here is that you called your
dealloc
methodrelease
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]
.