Sunday, June 16, 2013

NSFileWrapper readFromURL example in Objective C (iOS).


NSFileWrapper readFromURL

Recursively rereads the entire contents of a file wrapper from the specified location on disk.

- (BOOL)readFromURL:(NSURL *)url options:(NSFileWrapperReadingOptions)options error:(NSError **)outError

Parameters of [NSFileWrapper readFromURL]
url
URL of the file-system node corresponding to the file wrapper.
options
Option flags for reading the node located at url. See “File Wrapper Reading Options” for possible values.
outError
If an error occurs, upon return contains an NSError object that describes the problem. Pass NULL if you do not want error information.

Return Value of [NSFileWrapper readFromURL]
YES if successful. If not successful, returns NO after setting outError to an NSError object that describes the reason why the file wrapper could not be reread.

Discussion of [NSFileWrapper readFromURL]
When reading a directory, children are added and removed as necessary to match the file system.

NSFileWrapper readFromURL example.
If you're using NSDocument you can get the path of regular-file file wrappers with a little hack.

First create a NSFileWrapper subclass and overload the regular-file methods that receive a URL to store a copy of it.

@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

End of NSFileWrapper readFromURL example article.