0
votes

I have a function in my class:

-(void)receiveFileName:(NSNotification *) notification {
[self dismissViewControllerAnimated:YES completion:nil];
NSString *selectedFileURL = [notification.userInfo valueForKey:@"selectedFile"];
NSString *fileName = [[selectedFileURL lastPathComponent] stringByDeletingPathExtension];
NSData *fileData = [NSData dataWithContentsOfFile:selectedFileURL];

}

It receives a file destination url from a table view controller and i need it to produce a fileName string and an NSData but i did some debugging and it gets caught on the NSData part and gives me this error:

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

When i print the selectedFileURL i get

2012-08-14 21:58:01.309 Share Me[4546:907] file://localhost/var/mobile/Applications/B87D9131-2E73-4117-9EE8-32EA7E19127D/Documents/mza_9195653795305984944.320x480-75.jpg

And when i print the file name i get:

2012-08-14 21:58:01.309 Share Me[4546:907] mza_9195653795305984944.320x480-75.jpg

I tried using alloc and init but that didn't solve the problem. Any suggestions?

2

2 Answers

0
votes

verify the string variables (e.g. selectedFileURL) are not NSURLs. you can use isKindOfClass:, like this:

assert([selectedFileURL isKindOfClass:[NSString class]] && "not actually a string!");
0
votes

selectedFileURL is an NSURL instance, not an NSString - you probably misunderstood the docs of the framework/library you're using. Quick solutions:

NSData *fileData = [NSData dataWithContentsOfURL:selectedFileURL];

NSData *fileData = [NSData dataWithContentsOfFile:[selectedFileURL path]];

etc.

Hint: you could already know that the object was not a string but an URL object as it started with file:// - that's a special URL scheme for files; NSString instances containing a path don't begin with this prefix, they're of the plain form /path/to/file.ext.