Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

objective c NSURL may not respond to +initFileURLWithPath

I have two lines of code in the applicationDidFinishLaunching function:

NSString *targetFilePath = @"/Users/bob/Documents/About_Stacks.pdf";
NSURL *targetFileURL = [NSURL initFileURLWithPath:targetFilePath];

and I am getting the warning (title) in the second line...

I have no idea what I am doing wrong. This is an absurdly simply application... I have read other posts about reordering methods, but I am using classes provided by NS, nothing of my own.

Any advice would be much appreciated.

Thanks.

like image 610
caballo7 Avatar asked Jan 28 '26 16:01

caballo7


2 Answers

initFileURLWithPath: is an instance method, not a class method, so you have to create an instance of the class first with alloc. So:

NSString* targetFilePath = @"/Users/bob/Documents/About_Stacks.pdf";
NSURL* targetFileURL = [[NSURL alloc] initFileURLWithPath:targetFilePath];

If you want to use the convenience method, use fileURLWithPath:, so:

NSURL* targetFileURL = [NSURL fileURLWithPath:targetFilePath];
like image 200
Jason Coco Avatar answered Jan 31 '26 07:01

Jason Coco


You have to alloc an NSURL first.

NSURL *targetFileURL = [[NSURL alloc] initFileURLWithPath:targetFilePath];

If the method starts with "init", it means it should be called on an allocated instance, not on the class itself.

like image 29
Chris Cooper Avatar answered Jan 31 '26 09:01

Chris Cooper



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!