如何将类中存在的NSMutablearray保存到文件中?

时间:2016-10-23 00:33:18

标签: ios objective-c

Student.h

#import <Foundation/Foundation.h>
#import "Subject.h"

@interface Student : NSObject <NSCoding> {
    NSString *studentID;
    NSString *studentName;
    NSMutableArray<Subject* > *subjects;
}

@property (copy) NSString *studentID;
@property (copy) NSString *studentName;
@property (copy) NSMutableArray<Subject* > *subjects;

-(Student *)initWithStudentID:(NSString *)ID andStudentName:(NSString *)name;

-(void)addSubject:(Subject *) subject;
@end

Student.m

#import "Student.h"

@implementation Student

@synthesize  studentName;
@synthesize  studentID;
@synthesize  subjects;

-(Student *)initWithStudentID:(NSString *)ID andStudentName:(NSString *)name {

    Student *student = [[Student alloc] init];

    student.studentID = [NSString stringWithString:ID];
    student.studentName = [NSString stringWithString:name];

    return student;
}

-(void)addSubject:(Subject *)subject{
    if(subjects==nil){
        subjects=[[NSMutableArray alloc]init];
    }
    [subjects addObject:subject];


}

- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super init]) {
        [self setStudentID:[aDecoder decodeObjectForKey:@"studentID"]];
        [self setStudentName:[aDecoder decodeObjectForKey:@"studentName"]];
        [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:subjects] forKey:@"subjects"];
           }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:studentID forKey:@"studentID"];
    [aCoder encodeObject:studentName forKey:@"studentName"];
    [aCoder encodeObject:subjects forKey:@"subjects"];
}

@end

Subject.h

#import <Foundation/Foundation.h>

@interface Subject : NSObject
@property  NSString *subjectID;
@property  NSString *subjectName;
@end

Subject.m

#import "Subject.h"

@implementation Subject
@end

的main.m

#import <Foundation/Foundation.h>
#import "Student.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Student *student1=[[Student alloc ]initWithStudentID:@"Nirmal" andStudentName:@"101"];

        Subject *subject = [Subject alloc];
        subject.subjectID=@"S01";
        subject.subjectName=@"PHY";

        [student1 addSubject:subject];

        [NSKeyedArchiver archiveRootObject:student1 toFile:@"/Users/kuzhandaivel/Documents/nirmal.plist"];

        Student *student2=[NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/kuzhandaivel/Documents/nirmal.plist"];

        NSLog(@"%@",student2.studentID);
        NSLog(@"%@",student2.studentName);
        NSLog(@"%@",[student2.subjects description]);

    }
    return 0;
}

如何将类中存在的NSMutablearray保存到文件中? 上面的程序在以下行引发了以下错误:

[aCoder encodeObject:subjects forKey:@"subjects"];

在student.m文件中的encodeWithCoder函数。

Archiver[4667:102276] -[Subject encodeWithCoder:]: unrecognized selector sent to instance 0x1006000a0
2016-10-22 20:25:57.738 Archiver[4667:102276] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Subject encodeWithCoder:]: unrecognized selector sent to instance 0x1006000a0'
*** First throw call stack:

2 个答案:

答案 0 :(得分:0)

Subject也需要符合NSCoding协议,就像Student一样。

存档在您要归档的根对象中的每个类都必须符合NSCoding协议。

答案 1 :(得分:0)

对象层次结构中的所有对象都需要确认NSCoding协议能够保存到存档。

如果你看到NSString类

  

@interface NSString:NSObject exp&gt;

它实现了NSSecureCoding,它是NSCoding的子类。因此,您需要在子对象的所有子对象和子对象上实现NSCoding协议,依此类推。 它基本上遍历所有子对象并逐个编码。如果你没有在其中任何一个上实施,它将会失败。

  

Archiver [4667:102276] - [Subject encodeWithCoder:]:无法识别   选择器发送到实例0x1006000a0 2016-10-22 20:25:57.738   Archiver [4667:102276] *由于未捕获的异常而终止应用程序   &#39; NSInvalidArgumentException&#39;,原因:&#39; - [Subject encodeWithCoder:]:   无法识别的选择器发送到实例0x1006000a0&#39;   * 首先抛出调用堆栈:

我认为这一行说明了一切。

  

[subject encodeWithCoder:(NSCoder *)aCoder]