0

@界面

@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。我也遵守病人班的协议。

4

2 回答 2

1

确保父目录存在。据我所知,writeToFile:atomically:不会自动创建目录层次结构。

于 2012-05-03T04:40:57.087 回答
0

将您的 NSCoding 实现替换为以下内容:

- (id)initWithCoder:(NSCoder *)decoder{
    if (self = [super 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{
        [super encodeWithCoder: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];

 }

然后,更改行:

NSData *patientObjectArchived = [NSKeyedArchiver archivedDataWithRootObject:currentPatient];

和:

NSMutableData *patientObjectArchived = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData: patientObjectArchived];
[archiver encodeObject:currentPatient forKey:@"someKey"];
[archiver finishEncoding];
于 2012-05-03T06:29:35.193 回答