1

I have a NSMutableaArray of NSString objects. So i'm using NSKeyedArchiever to save it to disk. So when i try to use

- (void)encodeWithCoder:(NSCoder *)aCoder {
       [aCoder encodeObject:self.EventsList  forKey:@"Events"];  
 }

i got an error

   Event encodeWithCoder:]: unrecognized selector sent to instance 0x7fd06b542780

Here's my parts of code:

//-------------------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

I get textdata from Web-server using ASIFormDataRequest in JSON format, and then i add this object to NSMutableArray, which is also a Singleton, so it looks like this:

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

So, again, execution stops on this:

  [aCoder encodeObject:self.EventsList forKey:@"Events"];

But when i try to code single NSString object everything goes with no errors.

pavel_s
  • 465
  • 1
  • 7
  • 27
  • Did you already checked if the array is nil when you try to save? – LoVo Mar 30 '15 at 07:49
  • Yes, of course, when it passes to encodeWithCoder, array "EventsList" is not nil – pavel_s Mar 30 '15 at 07:50
  • I might be wrong but maybe the Singleton doesn't support NSCoding? – LoVo Mar 30 '15 at 07:55
  • well, i tried to create another class, which handled only Coding/Decoding, but i got the same error... – pavel_s Mar 30 '15 at 07:57
  • I think you need to implement NSCoding protocol inside your Event - Object object as well – LoVo Mar 30 '15 at 07:59
  • You mean @interface Event : NSObject ? I thought about it, but still got error :) – pavel_s Mar 30 '15 at 08:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/74078/discussion-between-lovo-and-pavel-s). – LoVo Mar 30 '15 at 08:00
  • It is your Model class that should comply to NSCoder not the singleton. Also, is there any specific reason that you are not calling `init` on `Event` instance object inside the `for` loop? – Gandalf Mar 30 '15 at 08:01
  • No specific reason, I added `init` to `Event` instance. You mean my `Event` class should comply to NSCoder? I added `` to it, doesn't help. – pavel_s Mar 30 '15 at 08:07

1 Answers1

1

eventList doesn't contain NSStrings, it contains Event objects.

Your Event class needs to implement encodeWithCoder: - as the exception message says, the Event class doesn't implement this method.

Also you should use a lowercase s for singleton as it is an instance, not a class, and you should probably not use singletons.

Paulw11
  • 108,386
  • 14
  • 159
  • 186