Sunday, June 16, 2013

NSFileWrapper removeFileWrapper example in Objective C (iOS).


NSFileWrapper removeFileWrapper

Removes a child file wrapper from the receiver, which must be a directory file wrapper.

- (void)removeFileWrapper:(NSFileWrapper *)child

Parameters of [NSFileWrapper removeFileWrapper]
child
File wrapper to remove from the directory.
Special Considerations
This method raises NSInternalInconsistencyException if the receiver is not a directory file wrapper.

NSFileWrapper removeFileWrapper example.
NSFileWrapper *oldFileWrapper = [self.fileWrapper.fileWrappers objectForKey:fileName];
if (oldFileWrapper) [self.fileWrapper removeFileWrapper:oldFileWrapper];

[self.fileWrapper addRegularFileWithContents:[self encodeObject:object]
                           preferredFilename:fileName];

Example of [NSFileWrapper removeFileWrapper].
- (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;
}

NSFileWrapper removeFileWrapper example.
- (NSFileWrapper *)fileWrapperOfType:(NSString *)typeName
                               error:(NSError **)outError
{

    if ([self documentFileWrapper] == nil) {
        NSFileWrapper *documentFileWrapper = [[NSFileWrapper alloc] initDirectoryWithFileWrappers:nil];
        [self setDocumentFileWrapper:documentFileWrapper];
    }

    NSDictionary *fileWrappers = [documentFileWrapper fileWrappers];
    NSFileWrapper *textFileWrapper = [fileWrappers objectForKey:@"text.txt"];
    NSFileWrapper *videoFileWrapper = [fileWrappers objectForKey:@"video.mp4"];

    if ((textFileWrapper == nil && [self text] != nil) || [self textHasChanged]) {
        if (textFileWrapper != nil)
            [documentFileWrapper removeFileWrapper:textFileWrapper];

        if ([self text] != nil) {
            NSData *textData = [[self text] dataUsingEncoding:NSUTF8StringEncoding];
            [documentFileWrapper addRegularFileWithContents:textData preferredFileName:@"text.txt"];
        }
    }

    if ((videoFileWrapper == nil && [self videoData] != nil) || [self videoHasChanged]) {
        if (videoFileWrapper != nil)
            [documentFileWrapper removeFileWrapper:videoFileWrapper];

        if ([self videoData] != nil)
            [documentFileWrapper addRegularFileWithContents:[self videoData] preferredFileName:@"video.mp4"];
    }

    return documentFileWrapper;
}

End of NSFileWrapper removeFileWrapper example article.