很抱歉,如果这很明显,但我正在努力解决如何从NSFileWrapper和docs were not particular enlightening.
中提取组件的问题。我有两个包装。一个人拥有一个简单的txt文件。另一个持有pageSettings。 PageSettings是一个NSObject,它包含几个NSStrings,BOOL等。这是我的包装器(基于Apple's Document-Based Application Programming Guide for iOS),我现在仍然试图再次提取信息:
if (self.fileWrapper == nil) {
self.fileWrapper = [[NSFileWrapper alloc] initDirectoryWithFileWrappers:nil];
}
NSDictionary *fileWrappers = [self.fileWrapper fileWrappers];
if (([fileWrappers objectForKey:@"page"] == nil) && (self.text != nil)) {
NSData *textData = [self.text dataUsingEncoding:NSUTF8StringEncoding];
NSFileWrapper *textFileWrapper = [[NSFileWrapper alloc] initRegularFileWithContents:textData];
[textFileWrapper setPreferredFilename:@"page"];
[self.fileWrapper addFileWrapper:textFileWrapper];
}
if (([fileWrappers objectForKey:@"pageSettings"] == nil) && (self.pageSettings != nil)) {
NSData *settingsData = [NSKeyedArchiver archivedDataWithRootObject:self.pageSettings];
NSFileWrapper *settingsWrapper = [[NSFileWrapper alloc] initRegularFileWithContents:settingsData];
[settingsWrapper setPreferredFilename:@"pageSettings"];
[self.fileWrapper addFileWrapper:settingsWrapper];
}
所以我的问题是如何将txt文件和pageSettings加载回self.text和self.pageSettings?
我使用- (NSData *)serializedRepresentation
吗?如果是这样,怎么样?或者我只是简单地反转它:
NSDictionary *fileWrappers = [self.fileWrapper fileWrappers];
self.text = [fileWrappers objectForKey:@"page"];
self.pageSettings = [fileWrappers objectForKey:@"pageSettings"];
我会非常感激任何解释。非常感谢。
答案 0 :(得分:6)
NSDictionary *fileWrappers = [self.fileWrapper fileWrappers];
NSFileWrapper *pageWrapper = [fileWrappers objectForKey:@"page"];
NSData *pageData = pageWrapper.regularFileContents;
self.text = [[NSString alloc] initWithData:pageData encoding:NSUTF8StringEncoding];
NSFileWrapper *pageSettingsWrapper = [fileWrappers objectForKey:@"pageSettings"];
NSData *pageSettingsData = pageSettingsData.regularFileContents;
self.pageSettings = [NSKeyedUnarchiver unarchiveObjectWithData:pageSettingsData];