【问题标题】:Save NSMutableArray of NSStrings to disk将 NSStrings 的 NSMutableArray 保存到磁盘
【发布时间】:2015-03-30 07:41:27
【问题描述】:

我有一个由 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 服务器获取文本数据,然后将此对象添加到 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 对象时,一切都没有错误。

【问题讨论】:

  • 您在尝试保存时是否已经检查过数组是否为 nil?
  • 是的,当然,当它传递给encodeWithCoder时,数组“EventsList”不是nil
  • 我可能错了,但也许 Singleton 不支持 NSCoding?
  • 好吧,我试图创建另一个类,它只处理编码/解码,但我得到了同样的错误......
  • 我认为您还需要在 Event - Object 对象中实现 NSCoding 协议

标签: ios objective-c xcode nsmutablearray nskeyedarchiver


【解决方案1】:

eventList 不包含NSStrings,它包含Event 对象。

您的Event 类需要实现encodeWithCoder: - 正如异常消息所说,Event 类没有实现此方法。

此外,你应该为单例使用小写的 s,因为它是一个实例,而不是一个类,你可能不应该使用单例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    • 2013-01-17
    • 2011-04-14
    • 1970-01-01
    相关资源
    最近更新 更多