Sunday, June 16, 2013

NSFileWrapper initDirectoryWithFileWrappers example in Objective C (iOS).


NSFileWrapper initDirectoryWithFileWrappers

Initializes the receiver as a directory file wrapper, with a given file-wrapper list.

- (id)initDirectoryWithFileWrappers:(NSDictionary *)childrenByPreferredName

Parameters
childrenByPreferredName
Key-value dictionary of file wrappers with which to initialize the receiver. The dictionary must contain entries whose values are the file wrappers that are to become children and whose keys are filenames. See “Working With Directory Wrappers” in Application File Management for more information about the file-wrapper list structure.

Return Value of [NSFileWrapper initDirectoryWithFileWrappers]
Initialized file wrapper for fileWrappers.

Discussion of [NSFileWrapper initDirectoryWithFileWrappers]
After initialization, the file wrapper is not associated with a file-system node until you save it using writeToURL:options:originalContentsURL:error:.

The receiver is initialized with open permissions: anyone can read, write, or modify the directory on disk.

If any file wrapper in the directory doesn’t have a preferred filename, its preferred name is automatically set to its corresponding key in the childrenByPreferredName dictionary.

NSFileWrapper initDirectoryWithFileWrappers example.
- (NSFileWrapper *)fileWrapperOfType:(NSString *)typeName
                               error:(NSError **)outError
{
    NSFileWrapper *dirWrapper = [[[NSFileWrapper alloc]
        initDirectoryWithFileWrappers:nil] autorelease];

    [dirWrapper addRegularFileWithContents:self.pdfData
        preferredFilename:@"MainDocument.pdf"];

    [dirWrapper addRegularFileWithContents:self.signatureBitmapData
        preferredFilename:@"SignatureBitmap.png"];

    …

    return dirWrapper;
}

Example of [NSFileWrapper initDirectoryWithFileWrappers].
- (NSFileWrapper *)fileWrapperOfType:(NSString *)typeName
                               error:(NSError **)outError;
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSFileWrapper * localizedWrapper =
    [[NSFileWrapper alloc] initDirectoryWithFileWrappers:localizedWrappers];
    [resourcesWrappers setObject:localizedWrapper
                          forKey:@"en.lproj"];
    NSFileWrapper * resourcesWrapper =
    [[NSFileWrapper alloc] initDirectoryWithFileWrappers:resourcesWrappers];
    [contentsWrappers setObject:resourcesWrapper
                         forKey:@"Resources"];
    NSFileWrapper * contentsWrapper =
    [[NSFileWrapper alloc] initDirectoryWithFileWrappers:contentsWrappers];
    // …
    for (id item in mumble) {
        NSString * filename = [item filename];
        NSData * data = [item data];
        [localizedWrapper addRegularFileWithContents:data
                                   preferredFilename:filename];
    }
    [contentsWrapper addRegularFileWithContents:[self infoPlistData]
                              preferredFilename:@"Info.plist"];
    [pool drain];
    [bundleWrappers setObject:contentsWrapper
                       forKey:@"Contents"];
    NSFileWrapper * bundleWrapper =
    [[[NSFileWrapper alloc] initDirectoryWithFileWrappers:bundleWrappers] autorelease];
    return bundleWrapper;
}

NSFileWrapper initDirectoryWithFileWrappers example.
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];
}

End of NSFileWrapper initDirectoryWithFileWrappers example article.