Saturday, April 27, 2013

NSFileHandle fileHandleForReadingFromURL example ios


fileHandleForReadingFromURL :error:

Returns a file handle initialized for reading the file, device, or named socket at the specified URL.
+ (id)fileHandleForReadingFromURL:(NSURL *)url error:(NSError **)error
Parameters
url
The URL of the file, device, or named socket to access.
error
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 [NSFileHandle fileHandleForReadingFromURL]
The initialized file handle object or nil if no file exists at url.
Discussion of [NSFileHandle fileHandleForReadingFromURL]
The file pointer is set to the beginning of the file. You cannot write data to the returned file handle object. Use thereadDataToEndOfFile or readDataOfLength: methods to read data from it.
When using this method to create a file handle object, the file handle owns its associated file descriptor and is responsible for closing it.
Example of [NSFileHandle fileHandleForReadingFromURL]

-(id) initWithURL:(NSURL *)remoteURL
{
    if (self = [super init])
    {
        NSError *err = nil;
        fileHandle = [NSFileHandle fileHandleForReadingFromURL:remoteURL error:&err];
        if (err)
        {
            NSLog(@"Error occurred, aborting. Details: %@", err);
            [self release];
            return nil;
        }
    }
}