Sunday, June 16, 2013

NSFileWrapper regularFileContents example in Objective C (iOS).


NSFileWrapper regularFileContents

Returns the contents of the file-system node associated with a regular-file file wrapper.

- (NSData *)regularFileContents

Return Value
Contents of the file-system node the file wrapper represents.

Discussion of [NSFileWrapper regularFileContents]
This method may return nil if the user modifies the file after you call readFromURL:options:error: or initWithURL:options:error: but before NSFileWrapper has read the contents of the file. Use the NSFileWrapperReadingImmediate reading option to reduce the likelihood of that problem.

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

NSFileWrapper regularFileContents 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 regularFileContents].
- (BOOL)readFromURL:(NSURL *)url error:(NSError **)outError;
{   
    self.fileWrapper = [[NSFileWrapper alloc] initWithURL:url options:nil error:outError];
    NSString *rtfFilename = [url lastPathComponent];
    NSFileWrapper *rtfWrapper = [self.fileWrapper.fileWrappers objectForKey:rtfFilename];
    NSString *rtfString = [[NSString alloc] initWithData:[rtfWrapper regularFileContents] encoding:NSUTF8StringEncoding];
    NSAttributedString *attributedString = [OUIRTFReader parseRTFString:rtfString];
   
    _text = [attributedString copy];
   
    return YES;
}

NSFileWrapper regularFileContents example.
- (BOOL)loadFileWrapperRepresentation:(NSFileWrapper *)wrapper ofType:(NSString *)type
{
    if ([type isEqualToString:@"Rich-Text With Attachments"]) {
        rtfData = [[NSAttributedString alloc]initWithRTFDFileWrapper:wrapper documentAttributes:nil];
    } else if ([type isEqualToString:@"Rich-Text"]) {
        rtfData = [[NSAttributedString alloc]
            initWithRTF:[wrapper regularFileContents] documentAttributes:nil];
    } else if ([type isEqualToString:@"Plain-Text"]) {
        fileContents = [[NSString alloc]
                initWithData:[wrapper regularFileContents] encoding:nil];
        rtfData = [[NSAttributedString alloc]initWithString:fileContents];
    }

    if (textView) {                                                               
        [[textView textStorage]replaceCharactersInRange:NSMakeRange(0, [[textView string] length])withAttributedString:rtfData];
        [rtfData release];                                                        
     }
     return YES;
}

- (NSFileWrapper *)fileWrapperRepresentationOfType:(NSString *)type
{
    NSRange range = NSMakeRange(0, [[textView string] length]);
    if ([type isEqualToString:@"Rich-Text With Attachments"]) {
        return [[textView textStorage] RTFDFileWrapperFromRange:range documentAttributes:nil];
    } else if ([type isEqualToString:@"Rich-Text"]) {
        NSFileWrapper * wrapper = [[NSFileWrapper alloc]
            initRegularFileWithContents:[textView RTFFromRange:range]];
        return [wrapper autorelease];
    } else if ([type isEqualToString:@"Plain-Text"]) {
        //haven't implemented plain-text saving yet
     }
}

End of NSFileWrapper regularFileContents example article.