Dennis
Dennis

Reputation: 3596

Objective C Exception during file reading?

I am trying to read a text file line by line, and the text file will be a really small file so I just used:

NSString *fileContents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

However, an exception is raised on that line, saying:

[NSURL getFileSystemRepresentation:maxLength:]: unrecognized selector sent to instance 0x7f92c40e1890

I'm really new to Objective-C and I don't get why this is happening...

Thanks in advance.


NSString *filePath;
NSOpenPanel *fileBrowser = [NSOpenPanel openPanel];
[fileBrowser setCanChooseFiles:YES];
[fileBrowser setCanChooseDirectories:YES];
if ([fileBrowser runModal] == NSOKButton) {
    NSArray *files = [fileBrowser URLs];
    for ( int i = 0; i < [files count]; i++ ) {
        filePath = [files objectAtIndex:i];
    }
}

Is this because of the [fileBrowser URLs] part? Thank you.

Upvotes: 1

Views: 732

Answers (2)

MarkPowell
MarkPowell

Reputation: 16540

You are getting this error because NSURL does not have a method getFileSystemRespresentation, this is in NSString.

You can either use this method on your fileContents string, or pass your fileContents string to NSURL's URLWithString method.

Upvotes: 1

rob mayoff
rob mayoff

Reputation: 385600

It looks like filePath is an NSURL, but stringWithContentsOfFile:encoding:error: expects the path as an NSString.

Try this:

NSString *fileContents = [NSString stringWithContentsOfURL:filePath encoding:NSUTF8StringEncoding error:nil];

Upvotes: 4

Related Questions