2

I have an array of URLs(Local Files).How can I get the filename without the extension.

So far I know how to get the filename with the extension.Is there an inbuilt function to do this.Something similar to https://msdn.microsoft.com/en-us/library/system.io.path.getfilenamewithoutextension(v=vs.110).aspx

var file_name = NSURL(fileURLWithPath: path_to_file).lastPathComponent!
rmaddy
  • 314,917
  • 42
  • 532
  • 579
techno
  • 6,100
  • 16
  • 86
  • 192
  • 2
    Have you looked at the documentation for the `URL` class? – rmaddy Sep 18 '17 at 17:04
  • @rmaddy The Answer to the linked question shows how to split the file path from the extension.I want to get the file name only. My question is-is there any inbuilt parameters or function to get the file name.This ideally should be achieved with a simple single line of code as in C#.It seems I need to do custom string manipulation. – techno Sep 18 '17 at 17:23
  • @rmaddy I Have gone through the documentation over here https://developer.apple.com/documentation/foundation/url .Its not helpful at all.There are few Topics related to Local Files.Thus this question. – techno Sep 18 '17 at 17:25
  • The linked question has many answers, not just one. Did you see [this one](https://stackoverflow.com/a/41386089/1226963)? – rmaddy Sep 18 '17 at 17:26
  • How is the documentation not helpful? It clearly show a method for removing the extension. And your question isn't specific to local files. It applies to any URL. – rmaddy Sep 18 '17 at 17:27
  • @rmaddy Its clear from the title `how-to-get-filename-without-extension-from-local-file-url-in-swift` ,that the question applies to local files. – techno Sep 18 '17 at 17:29
  • You missed my point. Any answer applies equally well to both local and remote URLs. Look at the specific answer I linked. It is exactly what you want. – rmaddy Sep 18 '17 at 17:31
  • @rmaddy Yes,but I have to go through every answer to find it out.So the question is not the exact duplicate.That's my point. – techno Sep 18 '17 at 17:32
  • @techno Dude, what's unhelpful? Just do a local search through the documentation page for `URL` for "extension" and before too long, you'll find a method called `.deletingPathExtension()`. Gee, I wonder what that does? – Charles Srstka Sep 18 '17 at 17:39
  • @CharlesSrstka Thing is why don't they put up any sample code ? or just a clean single function to do the job like in C#.You need to remove the extension and get the last path component :).I think SWIFT Documentation and the language itself needs to be improved further. – techno Sep 18 '17 at 17:46
  • @CharlesSrstka See how a helpful documentation looks like https://msdn.microsoft.com/en-us/library/system.io.path.getfilenamewithoutextension(v=vs.110).aspx – techno Sep 18 '17 at 17:47
  • @techno You mean like [this?](https://developer.apple.com/documentation/foundation/url/1780065-deletingpathextension) – Charles Srstka Sep 18 '17 at 17:48
  • @CharlesSrstka Where is the sample code or any clear explanation? Is the documentation intended only for experts? – techno Sep 18 '17 at 17:49
  • @techno What's unclear about "Returns a URL constructed by removing any path extension."? – Charles Srstka Sep 18 '17 at 17:50
  • @CharlesSrstka There are no examples for almost everything.Frankly-The Documentation SUCKS! – techno Sep 18 '17 at 17:55
  • `let filename = URL(fileURLWithPath: myFilePath).deletingPathExtension().lastPathComponent` – nicksweet Sep 18 '17 at 18:13
  • @nicksweet Thanks... I already got the answer :) – techno Sep 18 '17 at 18:15

0 Answers0