NSKeyedArchiver不写数据到文件...?

时间:2012-05-03 03:37:27

标签: iphone ios cocoa nskeyedarchiver

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

    }

在另一个实现中,我尝试归档患者对象。

     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。我也符合患者班的协议。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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];