Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if a string is an URL or a local path?

Tags:

swift

nsurl

I'm given a string. It will either be a local path to a file that exists or a fully qualified remote https url ending in a file component.

If it is a local path I want to use NSURL(fileURLWithPath: String).
If it is an URL, I want to use NSURL(string: String).

What is the simplest / most reliable way to decide which to use?

like image 253
i_am_jorf Avatar asked Sep 05 '25 03:09

i_am_jorf


1 Answers

A local file path string will always start with / assuming it is a full path (which it should be).

A remote https URL will start with https://

Simply check which prefix the string has.

like image 124
rmaddy Avatar answered Sep 07 '25 21:09

rmaddy