【发布时间】:2012-05-03 03:37:27
【问题描述】:
@接口
@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。我也符合患者类的协议。
【问题讨论】:
-
您可以通过使用
writeToFile:options:error:写入文件,然后检查NSError对象以了解详细信息,从而获得有关问题所在的更多信息。 -
createPatient: 做什么?如果这没有创建完整的写入路径,那就是你的问题。
-
createPatient 返回我的文件的完整路径。我 NSLoged 验证路径是否正确,确实如此。所以我认为问题不在于路径
标签: iphone ios cocoa nskeyedarchiver