@インターフェース
@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];
}
別の実装では、PatientObjectをアーカイブしようとします。
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ファイルにデータを書き込んでいますPath:... / Documents / PatientName/PatientObject.archive。患者クラスのプロトコルにも準拠しています。