NSFileWrapper addRegularFileWithContents
- (NSString *)addRegularFileWithContents:(NSData *)data preferredFilename:(NSString *)filename
Parameters
data
Contents for the new regular-file file wrapper.
filename
Preferred filename for the new regular-file file wrapper.
Return Value of [NSFileWrapper addRegularFileWithContents]
Dictionary key used to store the new file wrapper in the directory’s list of file wrappers. The dictionary key is a unique filename, which is the same as the passed-in 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.
Discussion of [NSFileWrapper addRegularFileWithContents]
This is a convenience method. The default implementation allocates a new file wrapper, initializes it with initRegularFileWithContents:, sends it setPreferredFilename:, adds it to the directory with addFileWrapper:, and returns what addFileWrapper: returned.
Special Considerations
This method raises NSInternalInconsistencyException if the receiver is not a directory file wrapper.
This method raises NSInvalidArgumentException if you pass nil or an empty value for filename.
NSFileWrapper addRegularFileWithContents example.
- (BOOL)readFromFileWrapper:(NSFileWrapper *)dirWrapper
ofType:(NSString *)typeName
error:(NSError **)outError
{
NSFileWrapper *wrapper;
NSData *data;
wrapper = [[dirWrapper fileWrappers] objectForKey:@"MainDocument.pdf"];
data = [wrapper regularFileContents];
self.pdfData = data;
wrapper = [[dirWrapper fileWrappers] objectForKey:@"SignatureBitmap.png"];
data = [wrapper regularFileContents];
self.signatureBitmapData = data;
…
return YES;
}
- (NSFileWrapper *)fileWrapperOfType:(NSString *)typeName
error:(NSError **)outError
{
NSFileWrapper *dirWrapper = [[[NSFileWrapper alloc]
initDirectoryWithFileWrappers:nil] autorelease];
[dirWrapper addRegularFileWithContents:self.pdfData
preferredFilename:@"MainDocument.pdf"];
[dirWrapper addRegularFileWithContents:self.signatureBitmapData
preferredFilename:@"SignatureBitmap.png"];
…
return dirWrapper;
}
ofType:(NSString *)typeName
error:(NSError **)outError
{
NSFileWrapper *wrapper;
NSData *data;
wrapper = [[dirWrapper fileWrappers] objectForKey:@"MainDocument.pdf"];
data = [wrapper regularFileContents];
self.pdfData = data;
wrapper = [[dirWrapper fileWrappers] objectForKey:@"SignatureBitmap.png"];
data = [wrapper regularFileContents];
self.signatureBitmapData = data;
…
return YES;
}
- (NSFileWrapper *)fileWrapperOfType:(NSString *)typeName
error:(NSError **)outError
{
NSFileWrapper *dirWrapper = [[[NSFileWrapper alloc]
initDirectoryWithFileWrappers:nil] autorelease];
[dirWrapper addRegularFileWithContents:self.pdfData
preferredFilename:@"MainDocument.pdf"];
[dirWrapper addRegularFileWithContents:self.signatureBitmapData
preferredFilename:@"SignatureBitmap.png"];
…
return dirWrapper;
}
Example of [NSFileWrapper addRegularFileWithContents].
NSFileWrapper *oldFileWrapper = [self.fileWrapper.fileWrappers objectForKey:fileName];
if (oldFileWrapper) [self.fileWrapper removeFileWrapper:oldFileWrapper];
[self.fileWrapper addRegularFileWithContents:[self encodeObject:object]
preferredFilename:fileName];
if (oldFileWrapper) [self.fileWrapper removeFileWrapper:oldFileWrapper];
[self.fileWrapper addRegularFileWithContents:[self encodeObject:object]
preferredFilename:fileName];
NSFileWrapper addRegularFileWithContents example.
- (NSFileWrapper *)fileWrapperOfType:(NSString *)typeName error:(NSError **)outError
{
NSFileWrapper *wrapper = [[[NSFileWrapper alloc] initDirectoryWithFileWrappers:nil] autorelease];
[wrapper addRegularFileWithContents:[@"please work" dataUsingEncoding:NSUTF8StringEncoding] preferredFilename:@"foobar"];
NSDictionary *metadata = [NSDictionary dictionaryWithObject:@"0.1" forKey:@"Version"];
NSString* errorDescription = nil;
NSData* dictionaryData = [NSPropertyListSerialization dataFromPropertyList:metadata format:NSPropertyListBinaryFormat_v1_0 errorDescription:&errorDescription];
if(!dictionaryData)
{
if(!errorDescription)
errorDescription = @"Unknown error";
if(outError)
*outError = [NSError errorWithDomain:@"YourErrorDomain" code:69 userInfo:[NSDictionary dictionaryWithObject:errorDescription forKey:NSLocalizedDescriptionKey]];
return nil;
}
[wrapper addRegularFileWithContents:dictionaryData preferredFilename:@"Info.plist"];
return wrapper;
}
{
NSFileWrapper *wrapper = [[[NSFileWrapper alloc] initDirectoryWithFileWrappers:nil] autorelease];
[wrapper addRegularFileWithContents:[@"please work" dataUsingEncoding:NSUTF8StringEncoding] preferredFilename:@"foobar"];
NSDictionary *metadata = [NSDictionary dictionaryWithObject:@"0.1" forKey:@"Version"];
NSString* errorDescription = nil;
NSData* dictionaryData = [NSPropertyListSerialization dataFromPropertyList:metadata format:NSPropertyListBinaryFormat_v1_0 errorDescription:&errorDescription];
if(!dictionaryData)
{
if(!errorDescription)
errorDescription = @"Unknown error";
if(outError)
*outError = [NSError errorWithDomain:@"YourErrorDomain" code:69 userInfo:[NSDictionary dictionaryWithObject:errorDescription forKey:NSLocalizedDescriptionKey]];
return nil;
}
[wrapper addRegularFileWithContents:dictionaryData preferredFilename:@"Info.plist"];
return wrapper;
}