Если вы используете NSDocument
, вы можете получить путь к оберткам файлов обычных файлов с небольшим взломом.
Сначала создайте подкласс NSFileWrapper
и перегрузите методы обычного файла, которые получают URL-адрес, чтобы сохранить его копию.
@implementation RMFileWrapper
- (id) initWithURL:(NSURL *)url options:(NSFileWrapperReadingOptions)options error:(NSError *__autoreleasing *)outError {
if (self = [super initWithURL:url options:options error:outError]) {
self.originalURL = url;
}
return self;
}
- (BOOL) readFromURL:(NSURL *)url options:(NSFileWrapperReadingOptions)options error:(NSError *__autoreleasing *)outError {
BOOL successful = [super readFromURL:url options:options error:outError];
if (successful) {
self.originalURL = url;
}
return successful;
}
@end
Затем добавьте эту NSFileWrapper
категорию:
@implementation NSFileWrapper(bestURLWithDocument)
- (NSURL*) bestURLInDocument:(SBDocument*)document {
if (document.fileURL && self.filename) {
NSString* path = [document.fileURL.path stringByAppendingPathComponent:self.filename];
return [NSURL fileURLWithPath:path];
} else if ([self isKindOfClass:[RMFileWrapper class]]) {
RMFileWrapper *fileWrapper = (RMFileWrapper*) self;
return fileWrapper.originalURL;
}
return nil;
}
@end
bestURLInDocument:
вернет URL-адрес узла файловой системы, если он доступен, или исходный URL-адрес файла, если нет.
Приведенный выше код предполагает, что вы не вкладываете оболочки файлов каталогов.