NSFileWrapper addFileWrapper
- (NSString *)addFileWrapper:(NSFileWrapper *)child
Parameters
child
File wrapper to add to the directory.
Return Value of [NSFileWrapper addFileWrapper]
Dictionary key used to store fileWrapper 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 addFileWrapper]
Use this method to add an existing file wrapper as a child of a directory file wrapper. If the file wrapper does not have a preferred filename, use the setPreferredFilename: method to give it one before calling addFileWrapper:. To create a new file wrapper and add it to a directory, use the addRegularFileWithContents:preferredFilename: method.
Special Considerations
This method raises NSInternalInconsistencyException if the receiver is not a directory file wrapper.
This method raises NSInvalidArgumentException if the child file wrapper doesn’t have a preferred name.
NSFileWrapper addFileWrapper 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;
}
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;
}
Example of [NSFileWrapper addFileWrapper].
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];
}