2012-05-03 5 views
0

@interfaceNSKeyedArchiverはデータをファイルに書き込んでいません...?別の実装では

@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ファイルのパスにデータを書き込んでいます。.../Documents/PatientName/PatientObject.archive。私は患者クラスのプロトコルにも順応しています。

+0

:で

NSData *patientObjectArchived = [NSKeyedArchiver archivedDataWithRootObject:currentPatient]; 

:行を変更し、その後、

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

をと正確に何が起こっているかについての詳細は、 'writeToFile:options:error:'ファイルを書き出し、 'NSError'オブジェクトを調べて詳細を調べます。 – aroth

+0

createPatientとは何ですか?それが完全なパスを作成していない場合は、あなたの問題です。 – rdelmar

+0

createPatientはファイルへのフルパスを返します。私はパスが正しいことを確認するためにそれをNSLogedし、確かにそれがあった。だから問題はパスだとは思わない – Michael

答えて

1

親ディレクトリが存在することを確認してください。私が覚えている限り、writeToFile:atomically:は自動的にディレクトリ階層を作成しません。

0

以下でごNSCoding実装を置き換えます。あなたが得ることができるかもしれない

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

NSCodingプロトコルに準拠していないNSObjectを継承していますが、それでもスーパーでinitWIthCoderを呼び出す必要はありますか? – Michael

関連する問題