Sunday, June 16, 2013

NSFileWrapper readFromURL options error example in Objective C (iOS).


NSFileWrapper readFromURL options error

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 options error]
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 options error]
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 options error]
When reading a directory, children are added and removed as necessary to match the file system.

NSFileWrapper readFromURL options error 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 options error example article.