我正在开发一个 OSX 应用程序并且需要保存自定义类的信息,所以我尝试使用 NSCoder 对其进行索引;但是,我试图在类中编码/解码的对象之一是 NSImage,每次我尝试运行 encodeWithCoder: 或 initWithCoder: 我都是以下两条错误消息之一:
Warning - attempting to encode NSCGSImageRep
Warning - attempting to decode NSCGSImageRep
这是我的.m:
@implementation Subject
-(id) init{
self = [super init];
if (self) {
self.ad = [[NSApplication sharedApplication] delegate];
self.editViewController = self.ad.editViewController;
self.name = @"";
self.teacher = @"";
self.mainPeriod = 1;
}
return self;
}
-(void) encodeWithCoder: (NSCoder *) coder{
[coder encodeObject:self.name forKey:@"name"];
[coder encodeObject:self.teacher forKey:@"teacher"];
NSNumber *periodNumBox = [NSNumber numberWithInt:self.mainPeriod];
[coder encodeObject:periodNumBox forKey:@"mainPeriod"];
[coder encodeObject:self.image forKey:@"image"];
}
-(id) initWithCoder: (NSCoder *) coder{
self.name = [coder decodeObjectForKey:@"name"];
self.teacher = [coder decodeObjectForKey:@"teacher"];
NSNumber *periodNumBox = [coder decodeObjectForKey:@"mainPeriod"];
self.mainPeriod = (int)[periodNumBox integerValue];
self.image = [coder decodeObjectForKey:@"image"];
return self;
}
@end
和我的.h:
#import <Foundation/Foundation.h>
#import "AppDelegate.h"
#import "EditViewController.h"
@class EditViewController;
@interface Subject : NSObject
@property (weak) AppDelegate *ad;
@property EditViewController *editViewController;
@property NSImage *image;
@property NSString *name;
@property NSString *teacher;
@property int mainPeriod;
@end