@界面
@interface Patient : NSObject <NSCoding>
@执行
- (id)initWithCoder:(NSCoder *)decoder{
self.patientPhoto = [decoder decodeObjectForKey:kPatientPhoto];
self.firstName = [decoder decodeObjectForKey:kFirstName];
self.lastName = [decoder decodeObjectForKey:kLastName];
self.age = [decoder decodeIntForKey:kAge];
self.photoPaths = [decoder decodeObjectForKey:kPhotoPaths];
self.photoDates = [decoder decodeObjectForKey:kPhotoDates];
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder{
[encoder encodeObject:self.patientPhoto forKey:kPatientPhoto];
[encoder encodeObject:self.firstName forKey:kFirstName];
[encoder encodeObject:self.lastName forKey:kLastName];
[encoder encodeInt:self.age forKey:kAge];
[encoder encodeObject:self.photoPaths forKey:kPhotoPaths];
[encoder encodeObject:self.photoDates forKey:kPhotoDates];
}
在不同的实现中,我尝试归档患者对象。
IMDataController* dataCtr = [IMDataController sharedDataController];
Patient *currentPatient = [[Patient alloc]init];
currentPatient.patientPhoto = self.photo.image;
currentPatient.firstName = self.firstName.text;
currentPatient.lastName = self.lastName.text;
currentPatient.age = [self.lastName.text intValue];
currentPatient.photoDates = nil;
currentPatient.photoPaths = nil;
NSString *patientNameForWriting = [self.firstName.text stringByAppendingString:self.lastName.text];
NSString *stringToPatientObjectFile = [dataCtr createPatient:patientNameForWriting];
NSFileManager* filemanager = [NSFileManager defaultManager];
NSData *patientObjectArchived = [NSKeyedArchiver archivedDataWithRootObject:currentPatient];
if([patientObjectArchived writeToFile:stringToPatientObjectFile atomically:YES]){
}else{
NSLog(@"Patient Object Didn't Archive");
}
if 语句中的上述方法未返回 YES。我已经对照 nil 检查了数据,我检查了我的编码器方法,但我找不到问题。我正在将数据写入 NSString 文件路径:.../Documents/PatientName/PatientObject.archive。我也遵守病人班的协议。