NSFileWrapper Basics: Extracting Components

Sorry if this is obvious, but I'm struggling to extract components from NSFileWrapper, and the docs were not particularly useful.

I have two wrappers. One contains a simple text file. In another, the Settings page. PageSettings is an NSObject that contains several NSStrings, BOOL, etc. This is my cover (based on Appleโ€™s iOS Application Programming Guide ) and I'm stuck trying to retrieve information again:

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

So my question is how to load the txt file and the Settings page back into self.text and self.pageSettings?

Do I use - (NSData *)serializedRepresentation ? If so, how? Or just change it like this:

 NSDictionary *fileWrappers = [self.fileWrapper fileWrappers]; self.text = [fileWrappers objectForKey:@"page"]; self.pageSettings = [fileWrappers objectForKey:@"pageSettings"]; 

I would really appreciate any explanation. Thank you very much.

+4
source share
1 answer
 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]; 
+6
source

All Articles