gpt4 book ai didi

iphone - NSKeyedArchiver 没有将数据写入文件...?

转载 作者:行者123 更新时间:2023-11-29 04:40:48 25 4
gpt4 key购买 nike

@界面

@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。我已经检查了数据是否为零,我已经检查了我的编码器方法,但找不到问题。我正在将数据写入 NSString 文件路径:.../Documents/PatientName/PatientObject.archive。我也遵守患者类别中的协议(protocol)。

最佳答案

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

关于iphone - NSKeyedArchiver 没有将数据写入文件...?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10424757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com