Sunday, June 16, 2013

NSFileWrapper initRegularFileWithContents example in Objective C (iOS).


NSFileWrapper initRegularFileWithContents

Initializes the receiver as a regular-file file wrapper.

- (id)initRegularFileWithContents:(NSData *)contents

Parameters
contents
Contents of the file.

Return Value
Initialized regular-file file wrapper containing contents.

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

The file wrapper is initialized with open permissions: anyone can write to or read the file wrapper. .

NSFileWrapper initRegularFileWithContents example.
- (NSFileWrapper*) fileWrapperOfType:(NSString *)typeName error:(NSError *__autoreleasing *)outError {
    return self.documentFileWrapper;
}

- (BOOL) readFromFileWrapper:(NSFileWrapper *)fileWrapper ofType:(NSString *)typeName error:(NSError *__autoreleasing *)outError {
    self.documentFileWrapper = fileWrapper;
    return YES;
}

- (void) addFileToDocumentFromURL:(NSURL*)fileURL {
    NSData* fileData = [NSData dataWithContentsOfURL:fileURL];
    NSFileWrapper *fileWrapper = [[NSFileWrapper alloc] initRegularFileWithContents:fileData];
    fileWrapper.preferredFilename = [fileURL lastPathComponent];
    [self.documentFileWrapper addFileWrapper:fileWrapper];
    [self updateChangeCount:NSChangeDone];
}

- (void) removeFileFromDocumentWithName:(NSString*)name {
    NSFileWrapper *fileWrapper = [self.documentFileWrapper.fileWrappers objectForKey:name];
    if (fileWrapper) {
        [self.documentFileWrapper removeFileWrapper:fileWrapper];
        [self updateChangeCount:NSChangeDone];
    }
}

- (NSFileWrapper*) documentFileWrapper {
    if (!_documentFileWrapper) { // New document
        _documentFileWrapper = [[NSFileWrapper alloc] initDirectoryWithFileWrappers:nil];
    }
    return _documentFileWrapper;
}

Example of [NSFileWrapper initRegularFileWithContents].
- (void)encodeObject:(id<NSCoding>)object toWrappers:(NSMutableDictionary *)wrappers preferredFilename:(NSString *)preferredFilename {
    @autoreleasepool {
        NSMutableData * data = [NSMutableData data];
        NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
        [archiver encodeObject:object forKey:@"data"];
        [archiver finishEncoding];
        NSFileWrapper * wrapper = [[NSFileWrapper alloc] initRegularFileWithContents:data];
        [wrappers setObject:wrapper forKey:preferredFilename];
    }

NSFileWrapper initRegularFileWithContents 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 initRegularFileWithContents example article.