Im trying to pass an array of 'Employee' objects iPhone to Apple Watch by serializing the array :
NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:employees];
and unserializing 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 do any changes on the Watch side to fix this error?
so I just had that exact same problem and the answer is simple but a little hard to find by oneself.
You simply have to use:
NSKeyedArchiver.setClassName("Employee", for: Employee.self)
NSKeyedUnarchiver.setClass(Employee.self, forClassName: "Employee")
wherever needed.
Looks like iOS extensions prefix the class name with the extension's name.
For me it was happening in my Today extension. What fixed it was adding @objc(MyExampleClass) before the declaration.
@objc(MyExampleClass)
open class MyExampleClass {
....
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With