Unable to decode object of class Employee for key (NS.object.0); a class can be defined in source code or a library that is not related

I am trying to pass an array of iPhone Employee objects to an Apple Watch by serializing the array:

NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:employees];

and non-esterizing it, as on the Watch side:

NSMutableArray *employees = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];

This is the Employee class:

@interface Employee : NSManagedObject
@property (nonatomic, retain) NSNumber * employeeID;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSNumber * age;
@property (nonatomic, retain) NSString * address;
@property (nonatomic, retain) NSString * designation;
@property (nonatomic, retain) NSString * teamName;
@property (nonatomic, retain) NSString * gender;
@property (nonatomic, retain) NSNumber * dateOfJoining;
@end

Do I have to make any changes to the Side Watch to fix this error?

+4
source share
2 answers

so I had the same problem and the answer is simple, but a little hard to find myself.

You just need to use:

  • NSKeyedArchiver.setClassName("Employee", forClass: Employee.self)
    before serialization
  • NSKeyedUnarchiver.setClass(Employee.self, for: "Employee")
    before deserialization

where necessary.

, iOS .

+20

teriiehina ; , .

: ​​ , Swift , :

DemoNote , , NSKeyedUnarchiver - .

className (, "CompanyDirectory", "CompanyDirectory.Employee", "Employee") , , , .

0

All Articles