So this is how you can use Apple’s Uniform Type Identifiers to detect the extension and MIME type of files in Swift without using path extension. The full code and tests are in the repo. Otherwise get returns everything after the last dot and set will replace the extension. Examples: /tmp/foo.txt => txt. To get the file extension in Swift, convert the path string to a URL using URL(fileURLWithPath:), and then use the pathExtension property. SWIFT files are meant to be opened and edited in Xcode or Swift Playgrounds, which have features for Swift development. You can also open and edit SWIFT files in any source code or text editor like Visual Studio Code or GitHub Atom.
In order to create an extension in Swift you add the word extension before the name. When navigating the symbols, press Up Arrow, Down Arrow, Left Arrow or Right Arrow. This String extension will get you the filename and extension of a file. Call it like below: let URLString = “Document.pdf”. URLString.fileName() returns Document and URLString.fileExtension() returns pdf.