NSFileWrapper setPreferredFilename
- (void)setPreferredFilename:(NSString *)filename
Parameters of [NSFileWrapper setPreferredFilename]
filename
Preferred filename for the receiver.
Discussion of [NSFileWrapper setPreferredFilename]
When a file wrapper is added to a parent directory file wrapper, the parent attempts to use the child’s preferred filename as the key in its dictionary of children. If that key is already in use, then the parent derives a unique filename from the preferred filename and uses that for the key.[NSFileWrapper setPreferredFilename]
When you change the preferred filename of a file wrapper, the default implementation of this method causes existing parent directory file wrappers to remove and re-add the child to accommodate the change. Preferred filenames of children are not preserved when you write a file wrapper to disk and then later instantiate another file wrapper by reading the file from disk. If you need to preserve the user-visible names of attachments, you have to store the names yourself.
Special Considerations
This method raises NSInvalidArgumentException if you pass nil or an empty value for filename.
NSFileWrapper setPreferredFilename 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];
}
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];
}
Example of [NSFileWrapper setPreferredFilename].
- (NSFileWrapper*) fileWrapperOfType:(NSString*) typeName error:
(NSError**) outError
{
NSData* mainContent = [self dataOfType:kDKDrawingDocumentType
error:outError];
if( mainContent == nil )
{
*outError = [NSError errorWithDomain:kOrteliusErrorDomain
code:kOrteliusNoDataErrorCode userInfo:nil];
return nil;
}
NSFileWrapper* fw = [[NSFileWrapper alloc]
initDirectoryWithFileWrappers:nil];
[fw addRegularFileWithContents:mainContent
preferredFilename:@"main_content"];
// add "Quick Look" preview:
NSFileWrapper* qlFolder = [[NSFileWrapper alloc]
initDirectoryWithFileWrappers:nil];
[qlFolder setPreferredFilename:@"QuickLook"];
[fw addFileWrapper:qlFolder];
[qlFolder release];
// add preview and thumbnail
NSData* preview = [[self drawing] thumbnailData];
[qlFolder addRegularFileWithContents:preview
preferredFilename:@"Thumbnail.jpg"];
if( mSavePreview )
{
preview = [[self drawing] pdf];
[qlFolder addRegularFileWithContents:preview
preferredFilename:@"Preview.pdf"];
}
return [fw autorelease];
}
(NSError**) outError
{
NSData* mainContent = [self dataOfType:kDKDrawingDocumentType
error:outError];
if( mainContent == nil )
{
*outError = [NSError errorWithDomain:kOrteliusErrorDomain
code:kOrteliusNoDataErrorCode userInfo:nil];
return nil;
}
NSFileWrapper* fw = [[NSFileWrapper alloc]
initDirectoryWithFileWrappers:nil];
[fw addRegularFileWithContents:mainContent
preferredFilename:@"main_content"];
// add "Quick Look" preview:
NSFileWrapper* qlFolder = [[NSFileWrapper alloc]
initDirectoryWithFileWrappers:nil];
[qlFolder setPreferredFilename:@"QuickLook"];
[fw addFileWrapper:qlFolder];
[qlFolder release];
// add preview and thumbnail
NSData* preview = [[self drawing] thumbnailData];
[qlFolder addRegularFileWithContents:preview
preferredFilename:@"Thumbnail.jpg"];
if( mSavePreview )
{
preview = [[self drawing] pdf];
[qlFolder addRegularFileWithContents:preview
preferredFilename:@"Preview.pdf"];
}
return [fw autorelease];
}