ios - 将 NSStrings 的 NSMutableArray 保存到磁盘

标签 ios objective-c xcode nsmutablearray nskeyedarchiver

我有一个 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,它也是一个单例,所以它看起来像这样:

    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 对象时,一切都没有错误。

最佳答案

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

您的 Event 类需要实现 encodeWithCoder: - 如异常消息所述,Event 类未实现此方法。

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

关于ios - 将 NSStrings 的 NSMutableArray 保存到磁盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29340789/

相关文章:

ios - Reactive Cocoa 上的应用程序崩溃订阅 [UIButton rac_signalForControlEvents :]

ios - 使用布局约束动画 UIView 交换

html - iOS 8.3 字段集边框错误

objective-c - 调试 SIGSEGV 崩溃

swift - 将 Xcode MVC 项目导出到 .framework 文件

ios - 如何在 iOS 中使用 SoundCloud SDK 请求轨道

ios - iPhone 应用程序设置只读值

c - "Save 38% now"C 字符串中止程序

iphone - OSX Mountain Lion 和 Xcode 4.4 中的网络链接调节器 Prefpane 在哪里

ios - Xcode 6.1 的 segue 操作列表中没有 "show"