salman siddiqui salman siddiqui - 1 year ago 69
Objective-C Question

Complete custom object not getting stored in NSUserDefault

I have custom object ProblemDescription with NSCoding protocol after storing object in NSUserDefault i am getting only two (patProbId,patientId)properties and rest are null -

@property (nonatomic, strong) NSString *patProbId;
@property (nonatomic, strong) NSString *patientId;
@property (nonatomic, strong) NSString *visitId;
@property (nonatomic, strong) NSString *doctorId;
@property (nonatomic, strong) NSString *description;
@property (nonatomic, strong) NSString *status;
@property (nonatomic, strong) NSString *diagonisedDate;
@property (nonatomic, strong) NSString *audModTs;

(id)initWithCoder:(NSCoder *)decoder {
self = [super init];
if (!self) {
return nil;
self.patProbId = [decoder decodeObjectForKey:Dict_patProbId];
self.patientId = [decoder decodeObjectForKey:Dict_patientId];
self.visitId = [decoder decodeObjectForKey:Dict_visitId];
self.doctorId = [decoder decodeObjectForKey:Dict_doctorId];
self.description = [decoder decodeObjectForKey:Dict_description];
self.status = [decoder decodeObjectForKey:Dict_status];
self.diagonisedDate = [decoder decodeObjectForKey:Dict_diagonisedDate];
self.audModTs = [decoder decodeObjectForKey:Dict_audModTs];

return self;

- (void)encodeWithCoder:(NSCoder *)encoder {

[encoder encodeObject:self.patProbId forKey:Dict_patProbId];
[encoder encodeObject:self.patientId forKey:Dict_patientId];
[encoder encodeBool:self.visitId forKey:Dict_visitId];
[encoder encodeBool:self.doctorId forKey:Dict_doctorId];
[encoder encodeBool:self.description forKey:Dict_description];
[encoder encodeBool:self.status forKey:Dict_status];
[encoder encodeBool:self.diagonisedDate forKey:Dict_diagonisedDate];
[encoder encodeBool:self.audModTs forKey:Dict_audModTs];


Answer Source

You are encoding Bool but decoding Object. You need to use same type for both encode and decode

EDIT: In comment, you mentioned that all properties are of NSString type, that is fine, but in that case, you need to encode them as Object

[encoder encodeBool:self.visitId forKey:Dict_visitId];

As you can see, you are encoding them as Bool, not as an Object. First two properties are fine, which is why they work while the rest is encoded as Bool which is why they don't work

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download