https://github.com/neoneye/swiftyrelativepath
Swift equivalent of Ruby's "Pathname.relative_path_from()"
https://github.com/neoneye/swiftyrelativepath
filemanager nsfilemanager nsurl swift url
Last synced: 2 months ago
JSON representation
Swift equivalent of Ruby's "Pathname.relative_path_from()"
- Host: GitHub
- URL: https://github.com/neoneye/swiftyrelativepath
- Owner: neoneye
- License: mit
- Created: 2018-01-20T22:13:55.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-01-21T02:44:28.000Z (over 7 years ago)
- Last Synced: 2025-03-21T09:35:20.480Z (2 months ago)
- Topics: filemanager, nsfilemanager, nsurl, swift, url
- Language: Swift
- Homepage:
- Size: 10.7 KB
- Stars: 9
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# SwiftyRelativePath
This is a Swift equivalent of Ruby's [`Pathname.relative_path_from()`](https://ruby-doc.org/stdlib-2.5.0/libdoc/pathname/rdoc/Pathname.html#method-i-relative_path_from)
or Java's [`Path.relativize()`](https://docs.oracle.com/javase/7/docs/api/java/nio/file/Path.html#relativize).Example of inputs and outputs:
| Long Path | Relative to Path | Return Value |
|---------------------------|------------------|-------------------|
| /usr/X11/agent/47.gz | /usr/X11 | agent/47.gz |
| /usr/share/man/meltdown.1 | /usr/share/cups | ../man/meltdown.1 |
| /var/logs/x/y/z/log.txt | /var/logs | x/y/z/log.txt |# Usage
import SwiftyRelativePath
let url0 = URL(fileURLWithPath: "/computer/qubit/17")
let url1 = URL(fileURLWithPath: "/computer/lab")
let path = url0.relativePath(from: url1)
// path is "../qubit/17"# Credits
- [Martin R](https://chat.stackoverflow.com/users/1187415/martin-r). Thank you for [your answer](https://stackoverflow.com/a/48360631/78336) on stackoverflow.
- [neoneye](https://github.com/neoneye)# License
MIT License.