Sunday, June 16, 2013

NSFileWrapper fileWrappers example in Objective C (iOS).

NSFileWrapper fileWrappers

Returns the file wrappers contained by a directory file wrapper.

- (NSDictionary *)fileWrappers

Return Value of [NSFileWrapper fileWrappers]
A key-value dictionary of the file wrappers contained in the directory. The dictionary contains entries whose values are the file wrappers and whose keys are the unique filenames that have been assigned to each one. See “Working With Directory Wrappers” in Application File Management for more information about the file-wrapper list structure.

Discussion of [NSFileWrapper fileWrappers]
Returns a dictionary whose values are the file wrapper's children and whose keys are the unique filenames that have been assigned to each one. This method may return nil if the user modifies the directory after you call readFromURL:options:error: or initWithURL:options:error: but before NSFileWrapper has read the contents of the directory. Use the NSFileWrapperReadingImmediate reading option to reduce the likelihood of that problem.

Special Considerations
This method raises NSInternalInconsistencyException if the receiver is not a directory file wrapper.

NSFileWrapper fileWrappers example.
NSDictionary *fileWrappers = [self.fileWrapper fileWrappers];
self.text = [[NSString alloc] initWithData:[[fileWrappers objectForKey:@"page"] regularFileContents] encoding: NSUTF8StringEncoding];
self.pageSettings = [NSKeyedUnarchiver unarchiveObjectWithData:[[fileWrappers objectForKey:@"pageSettings"] regularFileContents]];

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

[self.fileWrapper addRegularFileWithContents:[self encodeObject:object]

NSFileWrapper fileWrappers 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;

End of NSFileWrapper fileWrappers example article.