以下で取得可能。
path-to-me.swift
#!/usr/bin/env swift
import Foundation
guard let pathToMe = URL(string: "\(#file)", relativeTo: URL(fileURLWithPath: FileManager.default.currentDirectoryPath))?.path else {
print("Error: current path of #file")
exit(-1)
}
print( "path to me: \(pathToMe)" ) // path to me: /Users/uchcode/Desktop/path-to-me.swift
let __file__ = pathToMe as NSString
print( "NSString:")
print( " full path: \(__file__)" ) // full path: /Users/uchcode/Desktop/path-to-me.swift
print( " file name: \(__file__.lastPathComponent)" ) // file name: path-to-me.swift
print( " directory: \(__file__.deletingLastPathComponent)" ) // directory: /Users/uchcode/Desktop