NSKeyedArchiver 的问题

发布于 2024-09-19 04:56:11 字数 3251 浏览 11 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

筱果果 2024-09-26 04:56:11

两个明显的错误:

您在这里肯定做错的一件事是您调用了 dealloc 方法 release 。这不会起作用,并且会导致意外的行为。

另外,在 init 方法中,您应该执行 self = [super init] 而不仅仅是 [super init]

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].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文