Sunday, June 16, 2013

NSFileWrapper setFilename example in Objective C (iOS).


NSFileWrapper setFilename

Specifies the filename of a file wrapper.

- (void)setFilename:(NSString *)filename

Parameters of [NSFileWrapper setFilename]
filename
Filename of the file wrapper.

Discussion of [NSFileWrapper setFilename]
The file name is a dictionary key used to store fileWrapper in a directory’s list of child file wrappers. The dictionary key is a unique filename, which is the same as the child file wrapper's preferred filename unless that name is already in use as a key in the directory’s dictionary of children. See “Working With Directory Wrappers” in Application File Management for more information about the file-wrapper list structure. In general, the filename is set for you by the initWithURL:options:error:, initDirectoryWithFileWrappers:, or writeToURL:options:originalContentsURL:error: methods; you do not normally have to call this method directly.

Special Considerations
This method raises NSInvalidArgumentException if you pass nil or an empty value for filename.

NSFileWrapper setFilename example.
- (NSAttributedString *)pageFooter {                                                           
    NSString *imgName = @"Logo.tif";                                                           
    NSFileWrapper *fwrap = [[[NSFileWrapper alloc] initRegularFileWithContents:        
                 [[NSImage imageNamed:@"Logo"] TIFFRepresentation]] autorelease];
    [fwrap setFilename:imgName];
    [fwrap setPreferredFilename:imgName];

    NSTextAttachment * ta = [[[NSTextAttachment alloc] initWithFileWrapper:fwrap] autorelease];
    NSString *aString = @"Foo";
    NSMutableAttributedString *bString = [[NSMutableAttributedString alloc]initWithString:aString]];
    [bString appendAttributedString:[NSAttributedString attributedStringWithAttachment:ta]];
   [bString release];                                                                                 
    return bString;                                                                                    
}  

Example of [NSFileWrapper setFilename].
NSFileWrapper* filewrapper = [[NSFileWrapper alloc] init];
[filewrapper setFilename:filename];
NSTextAttachment* attachment = [[NSTextAttachment alloc] initWithFileWrapper:filewrapper]; 

NSFileWrapper setFilename example.
+(void)putIconForPath:(NSString*)path inTextView:(NSTextView*)textView {
    NSFileWrapper *filew = [[[NSFileWrapper alloc] initRegularFileWithContents:[NSData dataWithContentsOfFile:path]] autorelease];
    NSString* fileName = [path lastPathComponent];
    [filew setFilename:fileName];
    [filew setPreferredFilename:fileName];
   
    NSTextAttachment *fileAttatchment = [[[NSTextAttachment alloc] initWithFileWrapper:filew] autorelease];
    NSAttributedString *fileAttString = [NSAttributedString attributedStringWithAttachment:fileAttatchment];
   
    [textView insertText:fileAttString];
}

End of NSFileWrapper setFilename example article.