我有一个NSString对象的NSMutableaArray。所以我使用NSKeyedArchiever将其保存到磁盘。所以当我尝试使用
时- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.EventsList forKey:@"Events"];
}
我收到了错误
Event encodeWithCoder:]: unrecognized selector sent to instance 0x7fd06b542780
这是我的代码部分:
//-------------------Events.h--------------------------
@interface Event : NSObject
@property (strong,nonatomic) NSString *nameOfEvent;
@property (strong,nonatomic) NSString *dateOfEvent;
@property (strong,nonatomic) NSString *placeOfEvent;
@property int priorityOfEvent;
@end
//---------------Singleton.h ----------------
@interface GlobalSingleton : NSObject <NSCoding, NSCopying> {
NSMutableArray *EventsList;
}
@property (nonatomic,retain) NSMutableArray *EventsList;
+(GlobalSingleton *)sharedFavoritesSingleton;
@end
//----------------Singleton.m------------------------
....
@implementation GlobalSingleton
@synthesize EventsList;
....
....
- (void)encodeWithCoder:(NSCoder *)aCoder {
NSLog (@"%@",EventsList); // not nil
[aCoder encodeObject:self.EventsList forKey:@"Events"];
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super init])) {
NSMutableArray *temp = [[NSMutableArray alloc] initWithArray:[aDecoder decodeObjectForKey:@"Events"]];
self.EventsList = temp;
}
return self;
}
- (id)copyWithZone:(NSZone *)zone {
GlobalSingleton *copy = [[GlobalSingleton allocWithZone:zone] init];
copy.EventsList = self.EventsList;
return copy;
}
@end
我使用JSON格式的ASIFormDataRequest从Web服务器获取textdata,然后我将此对象添加到NSMutableArray,它也是一个Singleton,所以它看起来像这样:
NSDictionary *responseDict = [responseString JSONValue];
GlobalSingleton *Singleton = [GlobalSingleton sharedFavoritesSingleton];
for (NSDictionary *str in responseDict) {
Event *newEvent = [[Event alloc] init];
newEvent.nameOfEvent = [str objectForKey:@"EventName"];
newEvent.dateOfEvent = [str objectForKey:@"EventDate"];
newEvent.placeOfEvent = [str objectForKey:@"EventPlace"];
[Singleton.EventsList addObject:newEvent];
}
//------------------Save this data stored in NSMutableArray to disk-------------------------
[NSKeyedArchiver archiveRootObject:Singleton toFile:[self save_path]];
因此,执行再次停止:
[aCoder encodeObject:self.EventsList forKey:@"Events"];
但是当我尝试编写单个NSString对象时,一切都没有错误。
答案 0 :(得分:1)
eventList
不包含NSStrings
,它包含Event
个对象。
您的Event
类需要实现encodeWithCoder:
- 正如异常消息所示,Event
类没有实现此方法。
此外,你应该使用小写s作为单例,因为它是一个实例,而不是一个类,你可能不应该使用单例。