Reputation: 3596
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
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
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