简体   繁体   English

NSKeyedArchiver没有将数据写入文件…?

[英]NSKeyedArchiver NOT writing Data to File…?

@interface @接口

@interface Patient : NSObject <NSCoding>

@implementation @实现

- (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];

    }

In a different implementation I attempt to archive the Patient Object. 在另一种实现中,我尝试将患者对象存档。

     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");

     }

The above method within the if statement is not returning YES. if语句中的上述方法未返回YES。 I have checked the data against nil, I have checked my encoder method and I cannot find the problem. 我已经针对nil检查了数据,已经检查了编码器方法,但找不到问题。 I am writing the data to a NSString file Path:.../Documents/PatientName/PatientObject.archive. 我正在将数据写入NSString文件路径:... / Documents / PatientName / PatientObject.archive。 I am conforming to the protocol in the patient class also. 我也符合患者课堂中的协议。

Make sure the parent directory exists. 确保父目录存在。 As far as I can remember, writeToFile:atomically: does not create the directory hierarchy automatically. 据我所记得, writeToFile:atomically:不会自动创建目录层次结构。

Replace your NSCoding implementation with the following: 用以下内容替换您的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];

 }

And then, change the line: 然后,更改行:

NSData *patientObjectArchived = [NSKeyedArchiver archivedDataWithRootObject:currentPatient];

with: 与:

NSMutableData *patientObjectArchived = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData: patientObjectArchived];
[archiver encodeObject:currentPatient forKey:@"someKey"];
[archiver finishEncoding];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM