开发者

NSFileHandle fileHandleForReadingFromURL throws exception

开发者 https://www.devze.com 2023-03-11 00:47 出处:网络
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:

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/dataVer开发者_高级运维sion.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?


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

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

精彩评论

暂无评论...
验证码 换一张
取 消