2011-10-04 4 views
2

나는 NSManagedObjects를 포함하는 NSDictionary를 가지고 있습니다. NSKeyedArchiver를 사용하여 NSData 객체에이 값을 쓸 수 있습니다. 이것들은 this method.을 사용하여 생성됩니다.이 기능은 잘 작동하며 디스크의 일부 섹션을 디스크에 저장 한 다음 코어 데이터 모델에서 새로운 객체 세트로 다시 읽을 수있게 해줍니다. 나도 archivedDataWithRootObject 사용하는 경우 : 또는 archiveRootObject : 나는 NSPropertyListXMLFormat_v1_0 년에는 직렬화하려는 반면 나는, 아카이브의 형식이 NSPropertyListBinaryFormat_v1_0 것을 볼 수 documentation 에 따라 I 파일로 내 개체를 쓸 수 있도록, :, TOFILE 및 일반 old XML로 다른 곳에서 처리하십시오. (사실 Windows 기반 시스템에서 문서를 생성하고 싶습니다.)NSKeyedArchiver를 사용하여 NSDictionary에 보관 된 NSManagedObjects에서 XML을 생성합니다.

1) 어떻게 할 수 있습니까? 그렇다면 어떻게? 2) 더 나은 접근 방법이 있습니까?

파일을 나중에 iOS 장치로 보내고 개체 모델을 다시 만들고 싶기 때문에 직렬화 된 특성을 유지하고 싶습니다.

답변

3
  1. initForWritingWithMutableData:NSKeyedArchiver의 자신의 인스턴스를 만듭니다.
  2. setOutputFormat:NSPropertyListXMLFormat_v1_0으로 형식을 설정하십시오.
  3. 루트 개체를 encodeObject:forKey:으로 인코딩합니다.
  4. finishEncoding으로 전화하십시오.

이와 같이 인코딩 한 데이터의 보관을 취소하려면 마찬가지로 NSKeyedUnarchiver을 인스턴스화해야합니다.

1

고마워요! 나는 그 방향으로 나아가고 있었지만 그것이 올바른 방향인지 확신 할 수 없었다. 내가 누군가를 돕는 경우에 대비하여 코드에서 한 일이 여기 있습니다.

NSDictionary *dataAsDictionary=[self toDictionaryBlockingRelationships:blockRelationship]; 
NSString *savePath = [@"~/Documents/Saved.data" stringByExpandingTildeInPath]; 
NSMutableData *xmlData=[NSMutableData data]; 

NSKeyedArchiver *archive=[[NSKeyedArchiver alloc ]initForWritingWithMutableData:xmlData]; 

[archive setOutputFormat:NSPropertyListXMLFormat_v1_0]; 
[archive encodeRootObject:dataAsDictionary]; 
[archive finishEncoding]; 

if(![xmlData writeToFile:savePath atomically:NO]){ 
    NSLog(@"Failed to write to file to filePath=%@", savePath); 
} 
[archive release];