Jonas
Jonas

Reputation: 23

NSFileHandle fileHandleForReadingFromURL throws exception

I´m simply trying to read a text file on a server. For testing the file is local at the moment. The code looks like:

        NSString *pathToLocalFile = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/dataVersion.txt"];
NSURL *URLToLocalFile = [[NSURL alloc]initFileURLWithPath:pathToLocalFile];

if (![[NSFileManager defaultManager] fileExistsAtPath:pathToLocalFile]) {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager createFileAtPath:pathToLocalFile contents:nil attributes:nil];
}

NSError *err = nil;
//NSURL *url = [[NSURL alloc] initWithString:@"http://stackoverflow.com/tags"];
    //using url also doesn't work
localFile = [[NSFileHandle alloc]fileHandleForReadingFromURL:URLToLocalFile error:&err];
localFileData = [localFile readDataToEndOfFile];

The Problem is located here

[[NSFileHandle alloc]fileHandleForReadingFromURL:URLToLocalFile error:&err]; 

It always throws the following exception:

**NSInvalidArgumentException**, reason: **-[NSConcreteFileHandle fileHandleForReadingFromURL:error:]: unrecognized selector sent to instance.**  

The URLToLocalFile object seems to be okay.

What am I doing wrong here?

Upvotes: 2

Views: 606

Answers (1)

JeremyP
JeremyP

Reputation: 86651

It's a class method, you do not need to alloc an instance. Do this:

[NSFileHandle fileHandleForReadingFromURL:URLToLocalFile error:&err];

Upvotes: 3

Related Questions